2016-10-03 11 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

アップロードしたファイル名でファイルを移動し、名前を変更します。また、いくつかのファイルの種類のチェックとセキュリティをこのファイルに追加する必要があります..投稿を消毒する..ここで私はそれをやるでしょう。

<?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

upload.php

Iこの問題を解決するためにどれだけ。これは、あなたがそれを試してみることができます私の仕事: ちょうど

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

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

を変更します

関連する問題