2016-09-22 62 views
3

我已經在php上成功上傳圖片的圖片上傳腳本,但現在如果用戶再次上傳了同一個配置文件圖片,我想隨機重命名我有蘭特(1,100000),但它給了我錯誤。在php中隨機重命名相同的上傳配置文件圖片?

我的腳本是在這裏:

<?php 
 
$target_dir = "upload/"; 
 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
 
$uploadOk = 1; 
 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
 

 
if(isset($_POST["submit"])) { 
 
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
 
    if($check !== false) { 
 
     echo "File is an image - " . $check["mime"] . "."; 
 
     $uploadOk = 1; 
 
    } else { 
 
     echo "File is not an image."; 
 
     $uploadOk = 0; 
 
    } 
 
} 
 

 
if (file_exists($target_file)) { 
 
$_FILES['file']['name']=rand(1,100000).$_FILES['file']['name']; 
 
    $uploadOk = 0; 
 
} 
 

 
if ($_FILES["fileToUpload"]["size"] > 600000) { 
 
    echo "Sorry, your file is too large."; 
 
    $uploadOk = 0; 
 
} 
 

 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
 
&& $imageFileType != "gif") { 
 
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
 
    $uploadOk = 0; 
 
} 
 

 
if ($uploadOk == 0) { 
 
    echo "Sorry, your file was not uploaded."; 
 

 
} else { 
 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
 
     echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
 
    } else { 
 
     echo "Sorry, there was an error uploading your file."; 
 
    } 
 
\t 
 
\t if (is_file($target_file)) 
 
{ 
 
echo "Successful<BR/>"; 
 
echo "File Name :".$_FILES['fileToUpload']['name']."<BR/>"; 
 
echo "File Size :".$_FILES['fileToUpload']['size']."<BR/>"; 
 
echo "File Type :".$_FILES['fileToUpload']['type']."<BR/>"; 
 
echo "<img src=\"$target_file\" width=\"150\" height=\"150\">"; 
 
} 
 
} 
 
?>

而且我得到了以下錯誤:

enter image description here

回答

2

請在這樣這個功能的改變。

if (file_exists($target_file)) { 
    $target_file = $target_dir .rand(1,100000). basename($_FILES["fileToUpload"]["name"]); 
    $uploadOk = 1; 
} 
+0

Warning:move_uploaded_file(88627upload/14316802_10155315964098146_6539510858801534289_n.jpg):未能打開流:在第40行發生的C:\ wamp \ www \ upload \ index2.php中沒有這樣的文件或目錄 –

+0

您正在使用哪個oprationg系統? @RebeccaJoanna –

+0

檢查你的目標目錄路徑.. –

1

變化

if (file_exists($target_file)) { 
 
$_FILES['file']['name']=rand(1,100000).$_FILES['file']['name']; 
 
    $uploadOk = 0; 
 
}
if (file_exists($target_file)) { 
 
$_FILES['fileToUpload']['tmp_name']=rand(1,100000).$_FILES['fileToUpload']['tmp_name']; 
 
    $uploadOk = 0; 
 
}

+1

它提供了以下錯誤,現在的文件是一個圖像 - image/jpeg.Sorry,你的文件沒有上傳。 –

1

您需要從改變你的代碼:

if (file_exists($target_file)) { 
$_FILES['file']['name']=rand(1,100000).$_FILES['file']['name']; 
    $uploadOk = 0; 
} 

if (file_exists($target_file)) { 
    $target_file = $target_dir . rand(1,100000) . basename($_FILES["fileToUpload"]["name"]); 
    $uploadOk = 0; 
} 
1

使用下面的代碼

if (file_exists($target_file)) { 
$_FILES['fileToUpload']['tmp_name']=rand(1,100000).$_FILES['fileToUpload']['tmp_name']; 
    $uploadOk = 0; 
} 
+0

已經完成謝謝你我試過這個,但它給出了錯誤「File is一個圖像 - 圖像/ jpeg.Sorry,你的文件沒有上傳 - 「 –

+0

好吧。祝你有美好的一天。 –