php为什么不将图像内容写入磁盘? [英] how come php is not writing the image contents to disk?
问题描述
我有一些代码,该代码使用多部分表单和HTML5文件对象将图像(或电影,ppt等)上载到服务器,PHP在此将其接收并写入磁盘.但是,PHP似乎根本没有在写磁盘.
I have some code that uses a multi-part form and HTML5 file object to upload an image (or movie, ppt, etc) to a server, where PHP receives it and writes to disk. However, PHP doesn't appear to be writing to disk at all.
JavaScript:
Javascript:
function uploadFile (file, fileid) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
window["fn"+fileid] = xhr.responseText;
$("progress"+fileid).innerHTML = "<a href=\""+window["fn"+fileid]+"\">"+window["fn"+fileid]+"</a>";
}
}
var a = new Element("div");
a.id = "progress"+fileid;
a.setStyle("background-color", "#4682B4");
a.setStyle("height", "20px");
a.setStyle("width", "0px");
$("progress-wrapper").adopt(a);
xhr.upload.onprogress = function(e, a) {
var percent = Math.round((e.loaded*150)/e.total);
var acperct = Math.round(percent/1.5);
$("progress"+fileid).setStyle("width", percent);
$("progress"+fileid).innerHTML = file.name+" "+acperct+"%";
}
alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNNOPQRSTUVWXYZ1234567890".split("");
tmp_name = [];
for (i = 0; i < 6; i++) tmp_name[i] = alpha[Math.floor(Math.random() * alpha.length)];
xhr.open("POST", "upload.php", true);
xhr.setRequestHeader("Content-Type", "multipart/form-data");
xhr.setRequestHeader("size", file.size);
xhr.setRequestHeader("name", file.name);
xhr.setRequestHeader("type", file.type);
xhr.setRequestHeader("tmp_name", ""+tmp_name.join(""));
xhr.send(file);
}
PHP:
<?
function apache_request_headers() {
foreach($_SERVER as $key=>$value) {
if (substr($key,0,5)=="HTTP_") {
$key=str_replace(" ","-",ucfirst(strtolower(str_replace("_","_",substr($key,5)))));
$out[$key]=$value;
}else{
$out[$key]=$value;
}
}
return $out;
}
$headers = apache_request_headers();
$contents = file_get_contents("php://input");
echo $contents;
$ffilename=$headers["tmp_name"].$headers["name"];
$all = array('png','jpeg','jpg','gif','mov','txt','wmv','pdf');
$fh = fopen("upload/".$ffilename, "w+");
fwrite($fh, $contents);
fclose($fh);
echo "upload/".$ffilename;
?>
会发生什么情况,即书面文件或文本upload/
中什么都没有.
What happens is that there's either nothing in the written file or the text upload/
.
怎么了?
推荐答案
您没有指定在哪个平台上运行此代码,但是如果它是基于Unix的,请检查上载目录的权限-确保写入为apache运行的所有者/组设置了权限.作为快速测试,请执行chmod 0777 upload/
并查看文件是否显示.
You didn't specify which platform you are running this code on, but if it's unix-based, check the permissions on the upload directory - make sure that write permissions are set for the owner/group that apache runs as. As a quick test, do chmod 0777 upload/
and see if you file shows up.
这篇关于php为什么不将图像内容写入磁盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!