[英]PHP: allowing upload of .php files for users, how do I prevent them from running?
[英]Allowing users to upload files through website | PHP
好的,所以我按照一個教程講授了如何在我的網站上構建一個簡單的上傳系統。 好吧,我反而出錯了...
我有一個表格,允許用戶選擇他們要上傳的文件:
<form enctype="multipart/form-data" action="studentAccess/files/uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
這個應該執行所有工作的PHP腳本。 現在,我對該教程有些困惑,我不確定該腳本是否應該拆分為兩個不同的文件,或者應該與當前文件位於同一文件中。
<?php
// Where the file is going to be placed
$target_path = "/studentAccess/uploads/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$target_path = "/studentAccess/uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
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!";
}
?>
上面的腳本位於StudentAccess的Files
夾中
我要存儲上載文件的文件夾位於StudentAccess內,稱為uploads
studentAccess
|
| files
| |
| | uploader.php *this is where the script is located to upload file*
| |
| uploads
| |
| | *this is the folder where I want to store the uploaded files*
| |
上面是所有內容的文件夾布局。 之所以要包含此內容,是因為我不確定它的布局是否正確。 uploader.php
文件是否需要與uploader.php
文件位於同一文件夾中?
當我運行腳本時(即使用表單上傳文件並單擊“上傳文件”按鈕),我收到三個錯誤,它們直接顯示在瀏覽器窗口中。
Warning: move_uploaded_file(/studentAccess/uploads/NHSHandbook1314.doc) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/www/usd309bands.org/studentAccess/files/uploader.php on line 14
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpRpP6uz' to '/studentAccess/uploads/NHSHandbook1314.doc' in /home/www/usd309bands.org/studentAccess/files/uploader.php on line 14
最后是腳本本身生成的錯誤
There was an error uploading the file, please try again!
因此,我真的不確定自己在做什么錯,在我看來,將上載的文件移動到上載文件夾時遇到問題。 但是我真的不確定要解決該問題需要做什么。 我檢查了一下,並在服務器中打開了file_uploads
。
我希望我已經詳細描述了我的問題,以獲得幫助。 我希望它以可讀的格式進行布局。 我真的需要一些幫助。
它可能是文件夾允許的限制,請嘗試將目標文件夾設置為777(通過ftp程序進行讀取-寫入-執行),然后查看其是否有效。 我強烈建議您使用php的ftp函數上傳文件,而不要使用move_uploaded_file方法。
嘗試從$ target_path =“ / studentAccess / uploads /”更改目標路徑; 到$ target_path =“ ../uploads/”;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.