Home > Back-end >  Cannot upload image via move_uploaded_file
Cannot upload image via move_uploaded_file

Time:07-24

I am using code below to upload image via drag an drop system. Everything is ok, JS part is working well. But PHP part of code shows me error:

PHP Warning:  move_uploaded_file(assets/img/photos/1657614494809.jpg): failed to open stream: No such file or directory in /www_root/_inc/upload.php on line 29, referer: https://smartobchod.sk/bazar/add.php
[Sat Jul 23 12:56:18 2022] [error] [client 78.99.32.1] PHP Warning:  move_uploaded_file(): Unable to move '/home/gm016900/tmp/phpKaBL6m' to 'assets/img/photos/1657614494809.jpg' in /www_root/_inc/upload.php on line 29, referer: https://smartobchod.sk/bazar/add.php

My PHP code in upload.php is:

<?php

/* Getting file name */$filename = $_FILES['file']['name'];

/* Getting File size */$filesize = $_FILES['file']['size'];

/* Location */$location = "assets/img/photos/".$filename;

$return_arr = array();

/* Upload file */if(move_uploaded_file($_FILES['file']['tmp_name'],$location)){
    $src = "default.png";

    // checking file is image or not
    if(is_array(getimagesize($location))){
        $src = $location;
    }
    $return_arr = array("name" => $filename,"size" => $filesize, "src"=> $src);
}

echo json_encode($return_arr);

This is sent in header to upload.php:

Content-Disposition: form-data; name="file"; filename="1657614494809.jpg"
Content-Type: image/jpeg

Can you advice me what can be problem?

CodePudding user response:

Here I think your tmp/ folder permission is not proper so it is not able to read that folder or write just check /home/gm016900/tmp permission is should be gm016900 or it may not be created.

CodePudding user response:

Based on your comments, you just need to keep track of relative vs absolute paths.

I wasn't able to test this code, but hopefully you should get the general gist if it fails. Instead of a single $location variable, I'm using two variables $relativeLocation and $absoluteLocation. The latter isn't actually needed, but it makes debugging much easier.

/* Getting file name */
$filename = $_FILES['file']['name'];

/* Getting File size */
$filesize = $_FILES['file']['size'];

/* Location */
$relativeLocation = "assets/img/photos/".$filename;
$absoluteLocation = __DIR__.'/'.$relativeLocation;

$return_arr = array();

/* Upload file */
if (move_uploaded_file($_FILES['file']['tmp_name'], $absoluteLocation)) {
    $src = "default.png";

    // checking file is image or not
    if (is_array(getimagesize($absoluteLocation))) {
        $src = $relativeLocation;
    }
    $return_arr = array("name" => $filename, "size" => $filesize, "src" => $src);
}

echo json_encode($return_arr);

Depending on where your function lives relative to the storage, you might need to go update a directory or two, or possibly go up and then back down:

// Up one directory
$absoluteLocation = dirname(__DIR__).'/'.$relativeLocation;

// Up two directories
$absoluteLocation = dirname(__DIR__, 2).'/'.$relativeLocation;

// Up one directory then over to a sibling directory
$absoluteLocation = dirname(__DIR__).'/files/'.$relativeLocation;

As a personal preference, I always have a constant (or equivalent) in my projects that represents a known location on disk, and if I'm using a router script I define it there. That way, no matter how deep I get in nesting I can build my paths relative to that constant.

I also go one step further and use this library (merged into Symfony here) to avoid string concatenation, not worry about whether something ends with a slash or not, and to be more cross platform. So my actual code looks like:

$absoluteLocation = Path::join(PROJECT_ROOT, 'files', $relativeLocation);

Some people might think that's overkill, but once again it is just a personal preference.

  •  Tags:  
  • php
  • Related