-1

I have a simple PHP that will save a file to a specific folder. I already change the permission in 777 but I'm getting errors:

Warning: file_put_contents(/var/www/html/foldername/): failed to open stream: Is a directory on line 22
Warning: chmod(): Operation not permitted on line 23

How to fix these?

PHP

$file_name = '';
$file_binary = '';

$folder = "foldername";
$file_name = isset($_POST['file_name']) ? $_POST['file_name'] : '';
$file_binary = isset($_POST['file']) ? $_POST['file'] : '';
$file_directory = "/var/www/html/foldername/".$file_name;
file_put_contents($file_directory,str_replace("\\","",$file_binary));
chmod($file_directory,0777);
User014019
  • 1,215
  • 8
  • 34
  • 66
  • 2
    Are you sure, `"/path/foldername/".$file_name` is a valid path ? Never set permission to 777, 644 will be sufficient for files and 755 for folders. – Gaurav Sep 27 '16 at 05:09
  • @Gaurav, i already change the file path. – User014019 Sep 27 '16 at 05:12
  • Possible duplicate of [PHP - Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Oct 01 '16 at 12:13

3 Answers3

1

When there is a fileupload, you should get the data with $_FILE, not with $_POST. Try to change your code.

Igor Unger
  • 182
  • 2
  • 7
0

I think there are few correction need to be done in your code. first please add a default file name into your ternary operator, and instead of using file_put_content use fopen along with 'r+' please check the url for more options, and before creting a file need to check directory exist and is writable. Hope this will help you.

Ashish Ranade
  • 595
  • 2
  • 14
0

Try this,

$file_name = '';
$file_binary = '';

$folder = "foldername";
$file_name = isset($_POST['file_name']) ? $_POST['file_name'] : '';
$file_binary = isset($_POST['file']) ? $_POST['file'] : '';
//to create directory:

$folderPath = "/var/www/html/foldername";

is_dir($folderPath) || @mkdir($folderPath) || die("Can't Create folder");

$file_directory = "/var/www/html/foldername/".$file_name;
file_put_contents($file_directory,str_replace("\\","",$file_binary));
chmod($file_directory,0777);
Mr. J
  • 320
  • 3
  • 14