php - Issue with move_uploaded_file function - php

I am facing issue with the move_uploaded_file() php function below is my script.
Its saying permission denied . The directory is not writable . But I checked the permissions , its read-write-execute. Not Sure what's the issue , How do I make sure the permission is R-W-X. ?
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$target = "BharatTest/";
$photoName = basename( $_FILES['image']['name']);
$target = $target . basename( $_FILES['image']['name']) ;
ini_set('display_errors', 1);
echo '<pre>Debug: tmp file:', htmlspecialchars($_FILES['image']['tmp_name']), "</pre>\n";
echo '<pre>Debug: target directory: ', htmlspecialchars("BharatTest/"), "</pre>\n";
echo '<pre>Debug: real target: ', htmlspecialchars(realpath("BharatTest/")), "</pre>\n";
echo '<pre>Debug: source readable: ', is_readable($_FILES['image']['tmp_name']), "
</pre>\n";
echo '<pre>Debug: target is_dir: ', is_dir("BharatTest/") ? 'yes':'no', "</pre>\n";
echo '<pre>Debug: target writable: ', is_writeable("BharatTest/") ? 'yes':'no', "
</pre>\n";
if(move_uploaded_file($_FILES['image']['tmp_name'], $target))
{
echo "YES";
}
else
{
echo "NO";
}
?>

You need to ensure the webserver user has write permissions. Depending on the setup this often requires 775 or even 777 permissions on the folder.

Just because the directory is writable to you (when you log in), does not mean that it is writable to PHP / Apache / whatever web server you have. Depending on your server configuration, PHP likely runs with an entirely different set of permission. You can either lookup the permissions for your web server or set the directory to 777 and then backoff from there to determine the correct permissions for the directory.

Related

PHP fopen file not writable even after chmod set to 0777, Linux Server issue

Here is my simple code
$dst = "otps/hello.txt";
echo $dst, file_exists($dst) ? ' exists' : ' does not exist', "\n";
echo $dst, is_readable($dst) ? ' is readable' : ' is NOT readable', "\n";
echo $dst, is_writable($dst) ? ' is writable' : ' is NOT writable', "\n";
$fh = fopen($dst, 'w');
if ( !$fh ) {
echo ' last error: ';
echo '<pre>';print_r(error_get_last());echo'</pre>';
}
My current directory is set to 0777 but still the error, I created file manually and set permission to 0777 and still the same error.
here is output
Please note, I have setup my VPS server myself on digital ocean. I tried changing chown of the whole directories and files to apache, root, nobody but nothing is working.
running command
setenforce 0
works for me, not sure why :)

file_puts_content permission denied on Windows 7 WAMP

I am trying to use file_put_contents on my local website to download some images from a website and save them to the C:\ drive. However when running the script I get an error
file_put_contents(C:\product_images\A): failed to open stream: Permission denied
I have full permission for the product_images folder and also the A folder inside.
I know I could just chmod in ubuntu but not sure what I could do with Windows. I just right clicked and selected properties and made sure all the users had all the permissions applied
public function showImage() {
$product_image = ['/ABCD/ABCD.jpg','/ABCDE/ABCDE.jpg'];
foreach($product_image as $product_images) {
$url = "http://images.url.com/product_images" . $product_images ."";
$letter = substr($product_images, 1, 1);
$folder = 'C:\product_images\ ' .$letter . '';
$new_folder = str_replace(' ', '', $folder);
file_put_contents($new_folder, file_get_contents($url));
}
$success = "Success!";
return $success;
}
foreach($product_image as $product_images) {
$url = "http://images.com/product_images" . $product_images ."";
$test = explode("/", $product_images);
$folder = 'C:\product_images\ ' . $test[2] . '';
$new_folder = str_replace(' ', '', $folder);
$newer_folder = str_replace('/', '\\', $new_folder);
file_put_contents($newer_folder, file_get_contents($url));
echo '../product_images/' . $test[2] . '';
echo '<br />';
}
file_put_contents works with URLs only if allow_url_fopen is on ("1"). Check it with
ini_get("allow_url_fopen");
Also notice that while your writing folder might have the required permissions, the parent folder may NOT. Both (possible) folders should have the permissions.
You can also test the folder if its writable (through is_writable)

Php mkdir() is not creating a directory in my web directory

I can't make a new directory in my web server. I think my code is ok to create a directory. Can you tell me what is the error ?
$path = "https://wwww.domain.com/astuces/uploads/products/".$id;
if(!is_dir($path)){
mkdir($path);
if(mkdir($path)){
echo "mkdir is created successfully";
}else{
echo "directory is not created";
echo mysql_error();
}
}
It's always showing me "directory is not created".
This is not just a permission issue, you are doing impossible things.
$path = "https://wwww.domain.com/astuces/uploads/products/".$id;
You can never use a url as the path to create a directory in a server.
The path should be the path in your web server like:
$path = "/path/to/your/project/astuces/uploads/products/".$id;
And then make sure the apache user has the permission.
If the the parent directory also not exist at first, you have to set the third parameter to true of mkdir:
if(mkdir($path, 0755, true)){
Try this code
$path = "https://wwww.domain.com/astuces/uploads/products/".$id;
if(!is_dir($path)){
$old_umask = umask(0);
mkdir($path, true);
chmod($path, 0777);
umask($old_umask);
if(mkdir($path)){
echo "mkdir is created successfully";
}else{
echo "directory is not created";
echo mysql_error();
}
}

PHP - write to server folder denied

I have done chmod -R 777 on the root folder, but I'm still unable to successfully
upload (thus, write) to the uploaded folder!
Do I also have to change the php.ini file?
//$target_path = "http://localhost/photoServerProject/uploaded";
$target_path = "/photoServerProject/uploaded";
$fname = $_FILES["file"]["name"];
$upload_location = $target_path.'/'.$fname;
move_uploaded_file($_FILES["file"]["tmp_name"], $upload_location);
echo 'Moving file: ' . $fname . '</br></br>to: ' . $upload_location;
//echo "<img src=$upload_location>";
if(is_writeable($upload_location)){
echo '</br></br>Location <strong>is</strong> writeable ';
} else {
echo '</br></br>Location <strong>is NOT</strong> writeable ';
}
Output:
Moving file: camera.jpeg
to: /photoServerProject/uploaded/camera.jpeg
Location is NOT writeable
Try using
$targetPath= $_SERVER['DOCUMENT_ROOT'] . "photoServerProject/uploaded"
or
$targetPath= $_SERVER['DOCUMENT_ROOT'] . "/photoServerProject/uploaded"
I was confusing the difference between paths on my local drive vs server paths. My root folder (localhosts) for server paths is different than my local directory structure.
I was misunderstanding the difference between server and local disk directory structures. Namely, the root folders are different.
I'm surprised nobody brought up this issue.
Here's the solution:
<?php
$local_target = "~/webdev/photoServerProject/uploaded/";
$server_target = $_server['DOCUMENT_ROOT'] . "/photoServerProject/uploaded/";
$fname = $_FILES["file"]["name"];
$local_file_location = $local_target.$fname;
$server_file_location = $server_target.$fname;
move_uploaded_file($_FILES["file"]["tmp_name"], $local_file_location);
echo 'Moving file: ' . $fname . '</br></br>to local path: ' . $local_file_location;
echo '</br></br> But on the server it resides in : ' . $server_file_location;
echo '</br></br> See?';
echo "</br></br> <img src=$server_file_location>";
?>

fopen Permission denied on a file with 777 permissions

Since the title of this post is pretty much self-explanatory, I'll just jump to the code :
echo sprintf('%o', fileperms('test.txt'))."<br/>";
fopen("test.txt", "w");
And with this I get :
100777
fopen(test.txt): failed to open stream: Permission denied
Any ideas ?
Edit : Problem solved : there were access control lists on the server that were not configured correctly.
Thanks !
I think its possible that you have write/read permissions on the file but not on the folder. Try this in the public root of your website and see if you can read or write the file.
For safe mode (http://php.net/manual/en/function.fopen.php), php doc's say the following:
Note: When safe mode is enabled, PHP checks whether the directory in
which the script is operating has the same UID (owner) as the script
that is being executed.
Last you also need to be sure that php has access to the folder you are trying to write to.
I had same issue: folder was 777, but fopen does not worked. fopen said permission deny. Make sure your script have a 'good' permissions. maybe it will help you:
echo $dst, file_exists($dst) ? ' exists' : ' does not exist', "\n";
echo $dst, is_readable($dst) ? ' is readable' : ' is NOT readable', "\n";
echo $dst, is_writable($dst) ? ' is writable' : ' is NOT writable', "\n";
$fh = fopen($dst, 'w');
if ( !$fh ) {
echo ' last error: ';
var_dump(error_get_last());
}
I think the problem you are having is file ownership issue ... you can use this to find out the problem
error_reporting(E_ALL);
ini_set('display_errors','On');
$file = "a.jpg";
echo sprintf ( '%o', fileperms ( $file ) ), PHP_EOL;
echo posix_getpwuid ( fileowner ( $file ) ), PHP_EOL; // Get Owner
echo posix_getpwuid ( posix_getuid () ), PHP_EOL; // Get User
if (is_file ( $file )) {
echo "is_file", PHP_EOL;
;
}
if (is_readable ( $file )) {
echo "is_readable", PHP_EOL;
;
}
if (is_writable ( $file )) {
echo "is_readable", PHP_EOL;
}
fopen ( $file, "w" );
I just ran into this issue, and unfortunately the error message provided no clue to the actual reason. I had to give 777 to the file of the class included in the file that gave the error message. The error message only said the php file that calls that class, which already had 777.
So, check the file that is mentioned in the error message (let's say index.php), and then check which classes are instantiated within that file (class-file.php, class-writer.php, etc). Then check the permissions of those files (class-file.php, class-writer.php).
Once I gave permissions for the class file, it worked normally. Perhaps the webhost changed something in their config, since everything worked until a few days ago.

Categories