Move_uploaded_file :cannot open stream : no file or directory exists in Xampp Mac

 function upload_file($file)
     {
     if(isset($file))
      {
        $extension = explode('.', $file['name']);
        $new_name =rand() . '.' . $extension[1];
        $destination = 'p5/upload/' .$new_name;
        move_uploaded_file($file['tmp_name'], $destination);
        return $new_name;
      }
     }

I am not able to do anything… i have changed permissions for my folder but nothing is working.!! Can anyone help ? thanks

It returns me this error:

<br />
<b>Warning</b>:  move_uploaded_file(p5/upload/1428356704.jpg): failed to open stream: No such file or directory in <b>/Applications/XAMPP/xamppfiles/htdocs/p5/testdb.php</b> on line <b>83</b><br />
<br />
<b>Warning</b>:  move_uploaded_file(): Unable to move '/Applications/XAMPP/xamppfiles/temp/php90OmsL' to 'p5/upload/1428356704.jpg' in <b>/Applications/XAMPP/xamppfiles/htdocs/p5/testdb.php</b> on line <b>83</b><br />
Data Inserted

data is getting inserted in the database but it is not uploading to my htdocs/p5/upload/ directory

Can you try changing

$destination = 'p5/upload/' .$new_name;

to

$destination = $_SERVER['DOCUMENT_ROOT'] . '/p5/upload/' .$new_name;
1 Like

Notice: Undefined index: /Applications/XAMPP/xamppfiles/htdocs/ in /Applications/XAMPP/xamppfiles/htdocs/p5/testdb.php on line 83

Warning: move_uploaded_file(/p5/upload/1272171348.jpg): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/p5/testdb.php on line 84

Warning: move_uploaded_file(): Unable to move '/Applications/XAMPP/xamppfiles/temp/phpR0MBHV' to '/p5/upload/1272171348.jpg' in /Applications/XAMPP/xamppfiles/htdocs/p5/testdb.php on line 84
Data Inserted

Not working :frowning: sorry if i am wrong but the document root should be the directory of the destination folder directory right?

$_SERVER['DOCUMENT_ROOT'] doesn’t seem to be getting set/used for your destination file…

Can you copy/paste the function again so I can see the updated code?

1 Like

Sure

function upload_file($file)
     {
     if(isset($file))
      {
        $extension = explode('.', $file['name']);
        $new_name =rand() . '.' . $extension[1];
        $destination = $_SERVER['/Applications/XAMPP/xamppfiles/htdocs/'].'/p5/upload/'.$new_name;
        move_uploaded_file($file['tmp_name'], $destination);
        return $new_name;
      }
     }

I might hv made some mistake… its too confusing… to get to understand what is absolute and relative among my directories…

Yeah, that explains it.

When I put ‘DOCUMENT_ROOT’, I actually meant, use that.

Try using the following:

function upload_file($file)
     {
     if(isset($file))
      {
        $extension = explode('.', $file['name']);
        $new_name =rand() . '.' . $extension[1];
        $destination = $_SERVER['DOCUMENT_ROOT'].'/p5/upload/'.$new_name;
        move_uploaded_file($file['tmp_name'], $destination);
        return $new_name;
      }
     }

$_SERVER is a reserved set of keys that PHP will auto-fill for you at run time. They are called super globals.

1 Like

woah man that solved my problem … thank u so much… and sorry for the above mistake… u just made my day… i was tired figuring this out… Thanks again :slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.