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>
10 个解决方案
#1
32
File will be stored in temporary location, use tmp_name instead 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";
}
#2
13
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>";
?>
#3
12
Try using copy()
function instead of move_uploaded_file()
. It worked for me.
尝试使用copy()函数代替move_uploaded_file()。它为我工作。
copy($_FILES['file']['tmp_name'], $path);
#4
6
$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
检查上传目录是否可写
That's three.
这是三个。
Return Values
返回值
Returns TRUE on success.
成功返回TRUE。
If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.
如果filename不是有效的上传文件,则不会发生操作,而move_uploaded_file()将返回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.
如果文件名是一个有效的上传文件,但是由于某种原因无法移动,则不会发生任何操作,并且move_uploaded_file()将返回FALSE。此外,还将发出警告。
Look at return value of the function.
看看函数的返回值。
That's it.
就是这样。
#5
4
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 - r777 /var/www/my_project
#6
3
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);
#7
3
Posting this snipped that i wrote so that if someone else comes here wanting to upload multiple files would not return empty handed. :) This script does not perform any validation. It just does straight upload. Feel free to customize this basic code as your need.
发布我写的这个片段,这样如果有人想上传多个文件,就不会空手返回。:)此脚本不执行任何验证。它只是直接上传。您可以根据需要自定义这个基本代码。
HTML FORM
HTML表单
<form action="upload_file.php" method="POST" enctype="multipart/form-data">
Select images: <input type="file" name="files[]" multiple>
<input type="submit">
</form>
upload_file.php
upload_file.php
$move = "\uploads";
foreach ($_FILES["files"]["tmp_name"] as $key => $value)
{
$tmp_name = $_FILES["files"]["tmp_name"][$key];
$name = $move ."\\".basename($_FILES["files"]["name"][$key]);
move_uploaded_file($tmp_name, $name);
}
#8
2
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。
#9
2
You are not refering to the temporary location where 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.
如果您看到这个文件数组,您将会更好地理解和了解发生了什么。
#10
1
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首先进入;
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.
请注意,这不是在运行phpmyadmin和mysql的Linux环境中完成的。
#1
32
File will be stored in temporary location, use tmp_name instead 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";
}
#2
13
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>";
?>
#3
12
Try using copy()
function instead of move_uploaded_file()
. It worked for me.
尝试使用copy()函数代替move_uploaded_file()。它为我工作。
copy($_FILES['file']['tmp_name'], $path);
#4
6
$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
检查上传目录是否可写
That's three.
这是三个。
Return Values
返回值
Returns TRUE on success.
成功返回TRUE。
If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.
如果filename不是有效的上传文件,则不会发生操作,而move_uploaded_file()将返回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.
如果文件名是一个有效的上传文件,但是由于某种原因无法移动,则不会发生任何操作,并且move_uploaded_file()将返回FALSE。此外,还将发出警告。
Look at return value of the function.
看看函数的返回值。
That's it.
就是这样。
#5
4
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 - r777 /var/www/my_project
#6
3
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);
#7
3
Posting this snipped that i wrote so that if someone else comes here wanting to upload multiple files would not return empty handed. :) This script does not perform any validation. It just does straight upload. Feel free to customize this basic code as your need.
发布我写的这个片段,这样如果有人想上传多个文件,就不会空手返回。:)此脚本不执行任何验证。它只是直接上传。您可以根据需要自定义这个基本代码。
HTML FORM
HTML表单
<form action="upload_file.php" method="POST" enctype="multipart/form-data">
Select images: <input type="file" name="files[]" multiple>
<input type="submit">
</form>
upload_file.php
upload_file.php
$move = "\uploads";
foreach ($_FILES["files"]["tmp_name"] as $key => $value)
{
$tmp_name = $_FILES["files"]["tmp_name"][$key];
$name = $move ."\\".basename($_FILES["files"]["name"][$key]);
move_uploaded_file($tmp_name, $name);
}
#8
2
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。
#9
2
You are not refering to the temporary location where 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.
如果您看到这个文件数组,您将会更好地理解和了解发生了什么。
#10
1
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首先进入;
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.
请注意,这不是在运行phpmyadmin和mysql的Linux环境中完成的。