简体   繁体   English

使用php在Web服务器上写入文件

[英]Writing in file on web server using php

I have an android app that should upload photo to web server using php and POST connection, I receive the photo String correctly but my problem is with saving it in the server. 我有一个Android应用程序,应该使用php和POST连接将照片上传到Web服务器,我正确收到了照片字符串,但是我的问题是将其保存在服务器中。

the php file I am connecting on it is in the below location: 我连接的php文件位于以下位置:

www.mysite.com/project/code.php.

PHP is enabled on my website, and I enabled fopen command 我的网站上启用了PHP,并且启用了fopen命令

I also tried to open any file and write anything in it but I fail (Echo doesn't executed and no file is saved). 我也尝试打开任何文件并在其中写入任何内容,但失败了(Echo未执行且没有文件被保存)。 what is the problem? 问题是什么? should I have special permissions to be able to create files? 我应该具有创建文件的特殊权限吗?

Code: 码:

?php
// Get image string posted from Android App
$base=$_REQUEST['image'];
// Get file name posted from Android App
$filename = $_REQUEST['filename'];
// Decode Image
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploadedimages/'.$filename, 'wb');
// Create File
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete, Please check your php file directory';
?>

Check your Apache (or equivalent) logs to see if it is a permissions issue. 检查您的Apache(或等效文件)日志以查看是否存在权限问题。 If it is, you can set the owner of the folder to Apache from the linux command line with sudo chown www-data:www-data /uploadedimages/ and give owner-write permissions with chmod 0744 /uploadedimages/ . 如果是,则可以使用sudo chown www-data:www-data /uploadedimages/从linux命令行将文件夹的所有者设置为Apache,并使用chmod 0744 /uploadedimages/授予所有者写许可权。 That command also gives global and group read permissions, so you should double check if that is suitable. 该命令还提供了全局和组读取权限,因此您应仔细检查是否合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM