简体   繁体   English

move_uploaded_file() 不工作

[英]move_uploaded_file() not working

I am writing a script in php for the file upload on the server.我正在 php 中编写一个脚本,用于在服务器上上传文件。 And the code is as follows:代码如下:

$target_path = "uploaded_images/";
$target_path = $target_path . basename( $_FILES['image']['name']); 
if(move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {

  echo "The file ".  basename( $_FILES['image']['name']). 
        " has been uploaded";
  } else{
  echo "There was an error uploading the file, please try again!";
}

But the function move_uploaded_file() is not working and it gives the following error:但是 function move_uploaded_file()不起作用,它给出了以下错误:

Warning: move_uploaded_file(uploaded_images/Mordent.jpg) [function.move-uploaded-file]:  failed to open stream: Permission denied 


Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpOWVz7o' to 'uploaded_images/Mordent.jpg'

I have checked all the permissions to the folders and even the safe mode in php.ini is off.我检查了文件夹的所有权限,甚至 php.ini 中的安全模式已关闭。

Don't use a relative pathname ( uploaded_images/Mordent.jpg ) here.不要在此处使用相对路径名 ( uploaded_images/Mordent.jpg )。 It's not always obvious what PHP's working directory (which it will use to turn a relative path into an absolute one) is. PHP 的工作目录(它将用来将相对路径转换为绝对路径)是什么并不总是很明显。

If the directory you're trying to move the images to is in the same directory as your script, define $target_path like:如果您尝试将图像移动到的目录与您的脚本位于同一目录中,请定义$target_path如下:

$target_path = __DIR__ . '/uploaded_images/';

The __DIR__ part gives you the absolute path to the directory your current script is in, then you append the "relative" part to that. __DIR__部分为您提供当前脚本所在目录的绝对路径,然后为您提供 append 的“相对”部分。

Your code is perfectly fine...just change the permissions of the directory where you are moving the uploaded files您的代码非常好...只需更改您要移动上传文件的目录的权限

Like 0777 for all permissions of read and write you can do it manually or change it via php像 0777 一样,对于所有读写权限,您可以手动执行或通过 php 更改它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM