php Move_uploaded_file() 函数不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18929178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Move_uploaded_file() function is not working
提问by user2480054
I'm working on a website and I want the user to be able to upload files. So I'm trying to learn how to do that. I researched and it said that I had to use the function move_uploaded_file(). I wrote the code just like it was on the example (changing the data), but it wouldn't work. Please help me, I'm new at these. Here's what I've done so far:
我在一个网站上工作,我希望用户能够上传文件。所以我正在努力学习如何做到这一点。我研究了一下,它说我必须使用函数 move_uploaded_file()。我编写的代码就像在示例中一样(更改数据),但它不起作用。请帮助我,我是这些新手。这是我到目前为止所做的:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="upload_file.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type="file"name="file">
<input type="submit">
</form>
</body>
<html>
This is the upload_file.php:
这是upload_file.php:
<!DOCTYPE html>
<html>
<head>
<head>
<body>
<?php
$move = "/Users/George/Desktop/uploads/";
echo $_FILES["file"]['name']."<br>";
echo $_FILES["file"]['tmp_name']."<br>";
echo $_FILES["file"]['size']."<br>";
echo $_FILES['file']['error']."<br>";
move_uploaded_file($_FILES['file']['name'], $move);
?>
<body>
<html>
回答by Vlad Miller
File will be stored in temporary location, use tmp_nameinstead of name
文件将存储在临时位置,使用tmp_name而不是name
if (move_uploaded_file($_FILES['image']['tmp_name'], __DIR__.'/../../uploads/'. $_FILES["image"]['name'])) {
echo "Uploaded";
} else {
echo "File was not uploaded";
}
回答by user3502785
Try using copy()
function instead of move_uploaded_file()
. It workedfor me.
尝试使用copy()
function 而不是move_uploaded_file()
. 它的工作对我来说。
copy($_FILES['file']['tmp_name'], $path);
回答by Shankar Damodaran
This is a working example.
这是一个工作示例。
HTML Form :
HTML 表单:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
PHP Code :
PHP代码:
<?php
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
回答by Hanky Panky
$move = "/Users/George/Desktop/uploads/".$_FILES['file']['name'];
That's one.
那是一个。
move_uploaded_file($_FILES['file']['tmp_name'], $move);
That's two.
那是两个。
Check if the uploads
dir is writeable
检查uploads
目录是否可写
That's three.
那是三个。
Return Values
返回值
Returns TRUE on success.
If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.
If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.
成功时返回 TRUE。
如果 filename 不是有效的上传文件,则不会发生任何操作,并且 move_uploaded_file() 将返回 FALSE。
如果 filename 是有效的上传文件,但由于某种原因无法移动,则不会发生任何操作,并且 move_uploaded_file() 将返回 FALSE。此外,还会发出警告。
Look at return value of the function.
查看函数的返回值。
That's it.
就是这样。
回答by chirag ode
try this
尝试这个
$ImageName = $_FILES['file']['name'];
$fileElementName = 'file';
$path = 'Users/George/Desktop/uploads/';
$location = $path . $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $location);
回答by Joe Cheng
maybe you need to grant more permissions to your files.
也许您需要为您的文件授予更多权限。
suppose your code are under /var/www/my_project
假设您的代码在 /var/www/my_project 下
try chmod -R 777 /var/www/my_project
尝试 chmod -R 777 /var/www/my_project
回答by shivgre
You are not refering to the temporary locationwhere the file is saved.
您不是指保存文件的临时位置。
Use tmp_name
to access the file.
使用tmp_name
访问该文件。
You can always see what's getting posted using :
您始终可以使用以下命令查看发布的内容:
echo "<pre>";
print_r($_FILES);
If you see this files array you will have an better understanding and idea of what's going on.
如果您看到这个文件数组,您将对正在发生的事情有更好的理解和想法。
回答by Garry
it should like this
它应该像这样
move_uploaded_file($_FILES['file']['tmp_name'], $move);
And you cannot move it anywhere in your system .youcan move it in only in your project directory which must be in htdocs or www depends on what you are using wampp ,lampp or vertrgo.
并且您不能将它移动到系统中的任何位置。您只能将它移动到您的项目目录中,该目录必须位于 htdocs 或 www 中,具体取决于您使用的是 wampp、lampp 或 vertrgo。
回答by King Of The Jungle
This answer is late but it might help someone like it helped me
这个答案来晚了,但它可能会帮助像它帮助我的人
Just ensure you have given the user permission for the destination file
只需确保您已授予目标文件的用户权限
sudo chown -R www-data:www-data /Users/George/Desktop/uploads/
sudo chown -R www-data:www-data /Users/George/Desktop/uploads/
回答by Kevin RED
If you are on a windows machine, there won't be any problems with uploading or writing to the specified folder path, except the syntactical errors.
如果您在 Windows 机器上,上传或写入指定文件夹路径不会有任何问题,除了语法错误。
But in case of Linux users, there is a workaround to this problem, even if there are no syntactical errors visible.
但是对于 Linux 用户,即使没有可见的语法错误,也有解决此问题的方法。
First of all, I am assuming that you are using this in a Linux environment and you need to upload something to your project folder in the public directory.
首先,我假设您在 Linux 环境中使用它,并且您需要将某些内容上传到公共目录中的项目文件夹。
Even if you are having the write and read access to the project folder, PHP is not handled by the end user. It is and can be handled by a www-data
user, or group.
即使您拥有对项目文件夹的读写访问权限,最终用户也不会处理 PHP。它是并且可以由www-data
用户或组处理。
So in order to make this www-data
get access
first type in;
因此,为了使这个www-data
get access 首先输入;
sudo chgrp "www-data" your_project_folder
once its done, if there is no write access to the following as well;
一旦完成,如果没有对以下内容的写访问权限;
sudo chown g+w your_project_folder
That will do the trick in Linux.
这将在 Linux 中发挥作用。
Please, not that this is done in a Linux environment, with phpmyadmin, and mysql running.
请注意,这不是在 Linux 环境中完成的,运行 phpmyadmin 和 mysql。