I am new to PHP and AMPPS. I am trying a very simple file upload screen using this example:
我是PHP和AMPPS的新手。我正在使用此示例嘗試一個非常簡單的文件上傳屏幕:
http://www.tutorialspoint.com/php/php_file_uploading.htm
The HTML asks user to upload a file.
HTML要求用戶上傳文件。
Then the Action Calls and "uploader.php".
然后是Action Calls和“uploader.php”。
This uploader.php is supposed to move a file from source to target.
這個uploader.php應該將文件從源移動到目標。
file_uploads = On // In PHP INI file
file_uploads = On //在PHP INI文件中
upload_tmp_dir = "C:\Users\t_dutta\Documents\Projects\AMPPS\Domains\test-domain2\tempfiles"
upload_tmp_dir =“C:\ Users \ t_dutta \ Documents \ Projects \ AMPPS \ Domains \ test-domain2 \ tempfiles”
The time stamp of temp directory above does get updated every time I run the HTML form.
每次運行HTML表單時,上面臨時目錄的時間戳都會更新。
In the code, I have pasted various versions of copy function I tried.
在代碼中,我已經粘貼了我試過的各種版本的復制功能。
HTML Form (form.html) code:
enctype="multipart/form-data">
PHP Code (uploader.php):
$file_path = "\\temp";
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
$path = $root . "\\finalfiles\\";
$real_path = realpath($path);
if( $_FILES['file']['name'] != "" )
{
echo $_FILES['file']['name'] . "
echo $_SERVER['DOCUMENT_ROOT'] . "
echo $real_path . "
//Copy Function 1st Version
copy ($_FILES['file']['name'], $_SERVER["DOCUMENT_ROOT"]) or
die("
Could not copy file!
");//Copy Function 2nd Version
//copy ($_FILES['file']['name'], $root) or
// die("
Could not copy file!
");//Copy Function 3rd Version
//copy ($_FILES['file']['name'], $path) or
// die("
Could not copy file!
");//Copy Function 4th Version
//copy ($_FILES['file']['name'],"C:\Users\t_dutta\Documents\Projects\AMPPS\
//Domains\test-domain2\finalfiles") or die("
Could not copy file!
");}
else
{
die("No file specified!");
}
?>
Uploading CompleteUploaded File Info:
- Sent file: <?php echo $_FILES['file']['name']; ?>
- File size: <?php echo $_FILES['file']['size']; ?> bytes
- File type: <?php echo $_FILES['file']['type']; ?>
Error Message:
PN.txt
C:/Users/t_dutta/Documents/Projects/AMPPS/Domains/test-domain2
C:\Users\t_dutta\Documents\Projects\AMPPS\Domains\test-domain2\finalfiles
Warning: copy(PN.txt) [function.copy]: failed to open stream: No such file or directory in C:\Users\t_dutta\Documents\Projects\AMPPS\Domains\test-domain2\php\uploader.php on line 13
Could not copy file!
2 个解决方案
#1
0
This should work:
這應該工作:
// put this line after $root declaration (after line 2)
$target = $root . basename($_FILES["file"]["name"]);
// replace copy() function with this
(move_uploaded_file($_FILES["file"]["tmp_name"], $target))
The move_uploaded_file function is more relevant than using copy()
move_uploaded_file函數比使用copy()更相關
在這里閱讀更多相關信息
#2
0
I have now managed to fix the problem. The problem was indeed related to the parameters I passed to copy function as you indicated.
我現在設法解決了這個問題。問題確實與我指出的復制函數傳遞的參數有關。
I first changed the code from copy() to move_uploaded_file(). Then changed 1st argument from " $_FILES['file']['name'] " to " $_FILES['file']['tmp_name'] ". Then I modified the target path (in the 2nd argument) to how you suggested.
我首先將代碼從copy()更改為move_uploaded_file()。然后將第一個參數從“$ _FILES ['file'] ['name']”更改為“$ _FILES ['file'] ['tmp_name']”。然后我將目標路徑(在第二個參數中)修改為您的建議方式。
I built the target path this way (on Windows):
我以這種方式構建了目標路徑(在Windows上):
$path = $root . "\" . basename($_FILES['file']['name']) ;
$ path = $ root。 “\”。 basename($ _ FILES ['file'] ['name']);
Additional code: move_uploaded_file ($_FILES['file']['tmp_name'], $path)
附加代碼:move_uploaded_file($ _FILES ['file'] ['tmp_name'],$ path)
where $path = $root . "\" . basename($_FILES['file']['name']) ;
其中$ path = $ root。 “\”。 basename($ _ FILES ['file'] ['name']);
And it Worked !!! Thanks a lot for your help !!!
它工作了!!!非常感謝你的幫助 !!!