Reputation: 2541
I'm trying to upload an image using PHP using the function move_uploaded_file()
.
PHP
// Get image variables
$file = $_FILES['image']['tmp_name'];
$path = "images/products/large/";
$image_hash = "ty1bi"; // This would be generated
//I have also tried this: $path = "home/the_user/public_html/images/products/large/";
//$_SERVER['DOCUMENT_ROOT'] is equal to home/root_user/public_html/
// Upload image
if(!move_uploaded_file($file, $path . $image_hash . ".jpg")) {
header("location:admin/add.php?e=1");
exit;
}
At the moment, I'm getting:
Warning: move_uploaded_file(home/root_user/public_html/images/products/large/ty1bi.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/root_user/public_html/includes/functions.php on line 63
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpJSKsYA' to 'home/root_user/public_html/images/products/large/ty1bi.jpg' in /home/root_user/public_html/includes/functions.php on line 63
The hosting account was set up in WHM and I'm accessing the site via the /~the_user/
directory. So the site name would be either the main/root host domain name or the server IP address, followed by /~the_user
.
Because the error is showing the /root_user
which would be the username of the WHM host, I don't think it is able to access the /~the_user
. I have tried changing the document root in $path
so it starts from home/
and includes /the_user
but I'm also having no luck.
Is there a way to do this or will I have to use/test this on a domain name?
UPDATE
Because the document root was under the root username, the problem that was occurring was the correct build of the absolute path. I was sort of in the right direction by building the path, but this was absolute due to the missing slash at the beginning of the path string.
This line of code helped fixed the issue:
$path = "/home/the_user/public_html/images/products/large/";
Upvotes: 0
Views: 1854
Reputation: 360842
Makes me wonder about the fact that the errors is 'home/... without a leading /. could be a security setting somewhere stripping that off, making PHP think it should be putting the file into a subdir called home/... under your CWD.
Try
/home/.....
^---
instead
Upvotes: 1