简体   繁体   English

move_uploaded_file()在本地主机上运行,​​但在服务器上

[英]move_uploaded_file() working on localhost but on server

The following code works perfect on my localhost but it shows the following errors on my live server 以下代码在我的本地主机上可以正常工作,但在实时服务器上显示以下错误

Warning: move_uploaded_file(.../uploads/76948893.jpeg): failed to open stream: No such file or directory 警告: move_uploaded_file(... / uploads / 76948893.jpeg):无法打开流:无此类文件或目录

Warning: move_uploaded_file(): Unable to move '/tmp/phppxvRs8' to '.../uploads/76948893.jpeg' 警告: move_uploaded_file():无法将'/ tmp / phppxvRs8'移动到'... / uploads / 76948893.jpeg'

What it does is simple, it takes the images on the array ["pictures"] which comes from a html form and save every image on the folder ".../uploads/" using a random numeric name as name of the file and keeping the original extension. 它的操作很简单,它将图像从html表单中提取到数组[“ pictures”]上,并使用随机数字名称作为文件名将每个图像保存在文件夹“ ... / uploads /”中,并保留原始扩展名。

Any one knows how to make it work on my server? 有人知道如何使其在我的服务器上工作吗?

   //Image Uploader
    $images=[];
    $directory = '.../uploads/';
    foreach ($_FILES["pictures"]["error"] as $key => $error) {
        $new_file_name = rand (10000000,99999999);
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
       /* echo '<br>';
        echo $directory.$new_file_name.".".substr($_FILES['pictures']['type'][$key],strpos($_FILES['pictures']['type'][$key], "/")+1);*/

        if(move_uploaded_file($_FILES['pictures']['tmp_name'][$key], 
                            $directory
                            .$new_file_name
                            .".".substr($_FILES['pictures']['type'][$key],strpos($_FILES['pictures']['type'][$key], "/")+1)))               {
            array_push($images,$new_file_name.".".substr($_FILES['pictures']['type'][$key],strpos($_FILES['pictures']['type'][$key], "/")+1));
            $images_validator=true;
        }else{
        //Error
        }

    }
}

There could be many reason for this, Check the following 可能有很多原因,请检查以下内容

  1. You need WRITE Permission for the uploads directory. 您需要对uploads目录具有写权限。 I assume your local machine runs windows & your hosting environment is linux 我假设您的本地计算机运行Windows,并且您的托管环境是linux

  2. Like @Darren suggests, use absolute path. 就像@Darren建议的那样,使用绝对路径。 change the $directory to $directory = getcwd() . 'uploads/'; $directory更改为$directory = getcwd() . 'uploads/'; $directory = getcwd() . 'uploads/';

If this runs on your local machine but not on server there are 2 easy answers I can think off right off the back. 如果这是在您的本地计算机上运行,​​而不是在服务器上运行,那么我可以立即想到两个简单的答案。 1. The folder doesnt exist on the server, 2. as someone comment you dont have permission to write/read to that folder on the server....I would check the server configuration to make sure your app pool or users have read/write permissions to the folder 1.该文件夹在服务器上不存在,2.由于有人评论您没有写/读该服务器上的文件夹的权限....我将检查服务器配置,以确保您的应用程序池或用户已读/对该文件夹的写权限

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

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