无法在PHP中上传文件 [英] Unable to do File Upload in PHP
本文介绍了无法在PHP中上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用PHP上传文件.我写了
I want to upload files in PHP. I wrote
<form target="_self" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input name="submit" type="submit" value="submit" />
</form>
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
但是它不起作用.文件对我不可见,它们要去哪里?
but it's not working. Files are not visible to me, that where they are going?
推荐答案
您距离有效的脚本还有很长的路要走
You are a long way from a working script
您可以尝试
<?php
$output = array ();
$errors = array ();
$savePath = "uploaded"; // PATH to upload images
$allowedExt = array (
"png",
"jpg"
);
if (isset ( $_FILES ['file'] ) && $_FILES ["file"] ["error"] == UPLOAD_ERR_OK) {
$fileName = $_FILES ['file'] ['name'];
$fileSize = $_FILES ['file'] ['size'];
$fileTemp = $_FILES ['file'] ['tmp_name'];
$fileType = $_FILES["file"]["type"] ;
$fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
$fileExt = strtolower ( $fileExt );
//var_dump($fileExt);
if (!in_array ( $fileExt, $allowedExt )) {
$errors [] = "Invalid File Extention";
}
if ($fileSize > 800*1024) {
$errors [] = "File Too large";
}
if (! is_writable ( $savePath )) {
$errors [] = "File Destination not writeable";
}
$fileDst = $savePath . DIRECTORY_SEPARATOR . $fileName;
$filePrifix = basename ( $fileName, "." . $fileExt );
$i = 0;
while ( file_exists ( $fileDst ) ) {
$i ++;
$fileDst = $savePath . DIRECTORY_SEPARATOR . $filePrifix . "_" . $i . "." . $fileExt;
}
if (count ( $errors ) == 0) {
if (@move_uploaded_file ( $fileTemp, $fileDst )) {
$output['STATUS'] = "OK";
$output['TYPE'] = $fileType;
$output['Size'] = $fileSize;
$output['Destination'] = $fileDst;
} else {
$errors [] = "Error Saving File";
}
}
if(count($errors) > 0)
{
echo "<h2>Upload Error</h2>" ;
foreach ($errors as $error)
{
echo $error , "<br/>" ;
}
}
else
{
echo "<h2>File Uploaded</h2>" ;
foreach ($output as $key => $value)
{
echo $key , ":" , $value , "<br/>" ;
}
}
}
?>
<br/>
<br />
<form method="post" target="_self" enctype="multipart/form-data">
<input type="file" name="file" id="file" /> <input name="submit"
type="submit" value="submit" />
</form>
这篇关于无法在PHP中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文