Write a binary file in PHP

I wrote a PHP file that will save the JPEG file on the server, and part of the code is listed below:

//create folder if folder not exist if (!is_dir($save_path)){ $old = umask(0); $flag = @mkdir($save_path,0777); umask($old); if(isset($flag)){ $string = 'Folder Create Success!'."\n"; }else{ $string= 'Folder Create Fail!'."\n"; } echo $string; }else{ echo "Folder exist!!!!"; } //write the content to the server $base=$_REQUEST['image']; $binary=base64_decode($base); header('Content-Type: image/jpg; charset=utf-8'); if(!$file = fopen($path, 'wb')){ echo 'Image upload Fail!'."\n"; return; } else { fwrite($file, $binary); fclose($file); } 

The problem is that when I run the code, if the folder does not exist, it only creates the folder, but the contents cannot be saved in the folder. Error message:

[Thu Jul 05 16:59:06 2012] [error] [client 10.95.61.220] PHP Warning: fopen (/mnt/csis/upload/newphoto/others/12346_test/12346_test_2012-07-05_others_abc.jpg): failed to open stream: permission denied in / var / www / html / upload _image.php on line 57

However, if I run the code again since the folder was created in the past, it works correctly. Content can be saved in a folder ......

Anything I'm wrong? I am trying to find an answer on the Internet, but still can not solve the problem.

Anyone can help, thank you very much!

+4
source share
1 answer

I would try changing the folder creation to use the recursive flag :

 $flag = @mkdir($save_path . "/" . $file,0777,true); 
0
source

All Articles