用PHP编写二进制文件 [英] Write binary file in PHP

查看:140
本文介绍了用PHP编写二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个PHP文件,它将JPEG文件保存在服务器中,部分代码如下:

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 :

[2012年7月5日星期四] [错误] [客户端10.95.61.220] PHP警告:fopen(/mnt/csis/upload/newphoto/others/12346_test/12346_test_2012-07-05_others_abc.jpg):无法打开流:在第57行的/var/www/html/upload_image.php中,权限被拒绝

[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!

推荐答案

我会尝试将文件夹的创建更改为使用

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

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

这篇关于用PHP编写二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆