我从此页面复制了代码: http://www.w3schools.com/php/php_file_upload.asp
问题是我在上传文件时没有收到错误,而是这样 显示哪些应该是正确的:
上传:images.jpeg 输入:image / jpeg 大小:5.8603515625 kB 临时文件:/ tmp / phpZ67YXk 存储在:upload / images.jpeg
但是服务器上没有保存任何文件。
我不知道出了什么问题,但我在考虑许可方面,还有一个文件夹 具有777权限的命名上传。
这些php文件托管在在线网络主机上,因此我不会在本地运行。
HTML形式
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
upload_file.php
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_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 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
答案 0 :(得分:8)
我认为问题出在你的目的地的相对路径上(相对于你当前的工作目录=你的.php文件的路径),试着让它像这样绝对:
move_uploaded_file(
$_FILES["file"]["tmp_name"],
$_SERVER['DOCUMENT_ROOT'] . 'upload/' . $_FILES["file"]["name"]
);
答案 1 :(得分:1)
这有助于你:
chmod 777 foldername
php代码:
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$expensions= array("jpeg","jpg","png", "mp3",
"acc", "wav", "3gpp", "mp4", "3gp", "m4a", "amr", "avi",
"flv", "gif");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152666655){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"/var/www/upload/users/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
HTML:
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>