Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move_uploaded_file() failed to open stream: no such file or directory

I've done your standard checks (is the directory there, are lax enough permissions set), and I'm pretty sure I've covered your standard stupid human tricks. Here's the code that's failing:

move_uploaded_file($_FILES['image1']['tmp_name'], "/public_html/flashsale/assets/img/products/T".$_FILES['image1']['name']);

The directory is there - I copied the path from FileZilla. I even set the permissions to 777, both in FileZilla and in the file manager on the HostGator control panel. This code generates two warnings:

Message: move_uploaded_file(/public_html/flashsale/assets/img/products/Tsirloin.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory

Message: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpI5GZ3S' to '/public_html/flashsale/assets/img/products/Tsirloin.jpg'

In that order. So, the file is being uploaded, the directory exists and is set to 777, what else could I be missing?

like image 869
Fibericon Avatar asked Jan 30 '13 04:01

Fibericon


People also ask

What is Move_uploaded_file in PHP?

Definition and Usage The move_uploaded_file() function moves an uploaded file to a new destination. Note: This function only works on files uploaded via PHP's HTTP POST upload mechanism. Note: If the destination file already exists, it will be overwritten.

How to get upload file path in php?

php $dir = dirname(__FILE__); echo "<p>Full path to this dir: " . $dir . "</p>"; echo "<p>Full path to a . htpasswd file in this dir: " .


2 Answers

you do not need to put the full directory to the file. try to remove /public_html/flashsale/ from your link and see if that will work. In addition, the file does not need to have 777 permission, I myself upload files to folders with 755 permissions.

also, you can use getcwd(); in the directory your aiming to. the function will give you the directory that you need to use for moving your file. source

like image 91
syrkull Avatar answered Oct 05 '22 23:10

syrkull


The Problem

$dirpath = dirname(getcwd())

This is what I used initially to get the directory path to my /public_html/upload folder. $dirpath will contain

/public_html/upload

The Solution(On server)

$dirpath = realpath(dirname(getcwd()))

Since I’m on a shared hosting environment, the right way of getting move_uploaded_file to work is using this as the destination: realpath(dirname(getcwd())) returns something like:

/home/cpanelusername/public_html/upload
like image 34
UWU_SANDUN Avatar answered Oct 06 '22 00:10

UWU_SANDUN