2016-10-03 121 views
-1

對不起,我知道有很多關於這方面的文章,但我找不到解決方案。Php - move_uploaded_file不工作

這裏是我的形式:

<form id="form1" action="upload.php" method="post" enctype="multipart/form-data"> 
<table> 
    <tr> 
     <td>Name : </td> 
     <td><input type="text" id="name" name="name"/></td> 
    </tr> 
    <tr> 
     <td>Image :</td> 
     <td><input type="file" name="image"/></td> 
    </tr> 
    <tr><td id='submitAdd' colspan='2'><input type="submit" 
    value= " Add " /></td></tr> 
    </table> 
</form> 

這裏upload.php的:

<?php 
    $ext = strtolower(substr(strrchr($_FILES['image']['name'], '.'),1)); 
    $ret = move_uploaded_file($_FILES['image']['tmp_name'], 'item_images/'.$_POST['name'].'.'.$ext); 
    if ($ret) { 
     echo 'works'; 
    } 
    else { 
     echo 'doesnt work'."</br>"; 
     echo $_FILES['image']['error']; 
    } 
?> 

該目錄的權限都OK,沒有上傳錯誤,但它仍然不會移動文件。 我錯過了什麼嗎?

在此先感謝

回答

0

我認爲你必須指定絕對保存路徑,而不是它看起來像你現在擁有的相對路徑。

Ex。 dirname(__FILE__).'/item_images/'.$_POST['name'].'.'.$ext

0

我會在上傳的文件名下移動文件,然後重命名它。此外,一些文件類型檢查和安全性應該被添加到此..消毒後等。這是我該怎麼做。

upload.php的

<?php 
$fileName = $_FILES["image"]["name"]; // The file name 
$fileTmpLoc = $_FILES["image"]["tmp_name"]; // File in the PHP tmp folder 
$fileType = $_FILES["image"]["type"]; // The type of file it is 
$fileSize = $_FILES["image"]["size"]; // File size in bytes 
$fileErrorMsg = $_FILES["image"]["error"]; // 0 for false... and 1 for true 
$kaboom = explode(".", $fileName); // Split file name into an array using the dot 
$fileExt = end($kaboom); // Now target the last array element to get the file extension 
$fname = $kaboom[0]; 
$exten = strtolower($fileExt); 

//now we do some security checks 

if (!$fileTmpLoc) { // if file not chosen 
echo "ERROR: Please browse for a file before clicking the upload button."; 
exit(); 
} else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes 
echo "ERROR: Your file was larger than xxx Megabytes in size."; 
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder 
exit(); 
} else if (!preg_match("/.(gif|jpg|png)$/i", $fileName)) { 
// This condition is only if you wish to allow uploading of specific file types  
echo "ERROR: Your image was not .gif, .jpg, or .png."; 
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder 
exit(); 
} else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1 
echo "ERROR: An error occured while processing the file. Try again."; 
exit(); 
} 

//give it the new name 
$userstring= $_POST['name']; 
$string = $fname.$userstring.'.'.$exten; 
$image_name = preg_replace('/\s+/', '', $string); 

//now we move it. 

$moveResult = move_uploaded_file($fileTmpLoc, "item_images/$image_name"); 
// Check to make sure the move result is true before continuing 
if ($moveResult != true) { 
echo "ERROR: File not uploaded. Try again."; 
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder 
exit(); 
} 
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder 
$imageFile = "item_images/$image_name"; 
//$imageFile is the variable to use in the rest of your script. 

?> 
0

我如何解決這個問題。這爲我工作,你可以試試看: 只要改變

item_images/'.$_POST['name'].'.'.$ext);

'item_images/'basename($_FILES["image"]["name"])