4

I have wrote a PHP file which will save a JPEG file in the server and part of the code is listed as follow:

    //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 when I run the code, if the folder does not exist, it create the folder only but the content can't save in the folder. The error message is :

[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 work properly. The content can save in the folder......

Anything I get wrong? I try to find the answer on the web but still can't solve the problem.

Anyone can help, many thanks!

1 Answer 1

1

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

$flag = @mkdir($save_path . "/" . $file,0777,true);
Sign up to request clarification or add additional context in comments.

6 Comments

are you sure ? I see that you have both $path and $basepath - can you echo them and show me ?
if it still doesn't work, try to use chdir($path); before you open the file.
actually, the $path is the path of the file to be saved and $savepath is the path + file name. On the other hand, I have tried chdir($path) before open the file but the problem still exist...
but you're trying to do: fopen($path, 'wb') which means, according to your last remark, to open the directory...
eventually I have solved the problem by wait 10 second to save the content when a new folder created.....
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.