好吧,我正在嘗試創建一個網頁,將照片上傳到Apache htdocs中的文件夾。並且...呃...它不工作...我一直在尋找教程,並且整個事情都需要在PHP中完成(一些HTML也允許[不使用閃光])。PHP將文件上傳到文件夾(在apache中)
這裏是我....
<p>Browse For a File on your computer to upload it!</p>
<form enctype="multipart/form-data" action="upload_photos.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="250000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
<?PHP
if ($uploadedfile_size >250000)
{$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.<BR>";
$file_upload="false";}
else{
if (!($uploadedfile_type<>"image/jpeg" OR $userfile_type<>"image/tiff" OR $userfile_type<>"image/png"))
{$msg=$msg."Your uploaded file must be of JPG, PNG, or tiff. Other file types are not allowed<BR>";
$file_upload="false";}
}
?>
<!--
•enctype="multipart/form-data" - Necessary for our PHP file to function properly.
•action="upload_photos.php" - The name of our PHP page that was created.
•method="POST" - Informs the browser that we want to send information to the server using POST.
•input type="hidden" name="MA... - Sets the maximum allowable file size, in bytes, that can be uploaded. This safety mechanism is easily bypassed and we will show a solid backup solution in PHP. We have set the max file size to 100KB in this example.
•input name="uploadedfile" - uploadedfile is how we will access the file in our PHP script.
-->
</form>
</label>
</form>
現在,它不會越過這個點,但是一旦進入到其他頁面(upload_photos.php)它說:「有一個上傳文件時出錯, 請再試一次!」
<?php
/*
When the uploader.php file is executed, the uploaded file exists in a temporary storage area on the server. If the file is not moved to a different location it will be destroyed! To save our precious file we are going to need to make use of the $_FILES associative array.
The $_FILES array is where PHP stores all the information about files. There are two elements of this array that we will need to understand for this example.
•uploadedfile - uploadedfile is the reference we assigned in our HTML form. We will need this to tell the $_FILES array which file we want to play around with.
•$_FILES['uploadedfile']['name'] - name contains the original path of the user uploaded file.
•$_FILES['uploadedfile']['tmp_name'] - tmp_name contains the path to the temporary file that resides on the server. The file should exist on the server in a temporary directory with a temporary name.
*/
// Where the file is going to be placed
$target_path = "uploads/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);
/*
Now all we have to do is call the move_uploaded_file function and let PHP do its magic. The move_uploaded_file function needs to know 1) The path of the temporary file (check!) 2) The path where it is to be moved to (check!).
*/
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename($_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
/*
If the upload is successful, then you will see the text "The file filename has been uploaded". This is because move_uploaded_file returns true if the file was moved, and false if it had a problem.
If there was a problem then the error message "There was an error uploading the file, please try again!" would be displayed.
*/
?>
請幫助我,我一直在逐行瀏覽它,我不知道它爲什麼這樣做。這可能是一個語法錯誤或什麼的。我是全新的PHP,所以我不懷疑它。無論如何,提前感謝。
將$ target_path更改爲完整的服務器路徑。 – 2012-02-16 19:43:43
你有什麼相關的Apache設置? Apache也可以對文件上傳的類型和大小施加限制。你的Apache錯誤日誌說什麼? – 2012-02-16 19:43:44
「我是PHP新品」---這是學習如何調試**代碼的好機會。沒有人會爲你做這件事,那是你作爲程序員的日常工作 – zerkms 2012-02-16 19:44:38