图片上传表单适用于部分但不适用于其他部分

时间:2012-03-27 02:41:48

标签: php image forms upload

我有一张效果很好的图片上传表单!对我来说......似乎几乎所有人都会收到错误!这个世界怎么样? (我知道我的编程很草率,我不是在寻找批评,只是快速修复!请帮忙。)

形式:

<form enctype="multipart/form-data" action="imgupload.php" method="POST">
Image upload:
<input name="uploadedfile" type="file" />
<input type="submit" value="Upload" />
</form>

表单处理程序:

<?php

if (($_FILES["uploadedfile"]["type"] == "image/jpeg")
&& ($_FILES["uploadedfile"]["size"] < 4000000))
{


  // Where the file is going to be placed 
  $target_path = "/images/";


  /* Add the original filename to our target path.  
  Result is "images/filename.extension" */

//////////RENAME FILE/////////////


$filetype = pathinfo($_FILES['uploadedfile']['name'], PATHINFO_EXTENSION);  
$oldtitle =  $_FILES["uploadedfile"]["name"];
$take_file_extension = substr($oldtitle, 0, strrpos($oldtitle, '.'));
$title = "jeep_".$take_file_extension.".".$filetype;


  $target_path = $target_path . $title;


if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

  echo "The picture ".  basename( $_FILES['uploadedfile']['name']). 
  " has been uploaded<br><br>----------------<br><br>";

  echo "Upload: " . $_FILES["uploadedfile"]["name"] . "<br>";
  echo "Size: " . ($_FILES["uploadedfile"]["size"] / 1024) . " Kb<br>";

    }
}



else
{
    echo "There is a file size limit of 1MB. Only JPG's can be uploaded!";
}
?>

每次都适合我,其他人都知道“文件大小限制为1MB。只能上传JPG!”错误。

2 个答案:

答案 0 :(得分:0)

即使您的代码设置为接受最大4MB的图像,您也必须通过编辑一些php.ini设置的值来告诉PHP允许这种大小的图像:

您之所以只能上传.jpg图片是因为您的条件仅限于.jpg:

if (($_FILES["uploadedfile"]["type"] == "image/jpeg")

尝试开关:

switch ($_FILES["uploadedfile"]["type"]) {

    case 'image/jpeg':
    case 'image/png':
        break;

    default:
        throw new Exception('Invalid image type');
}

答案 1 :(得分:0)

我想知道它是否是你在php.ini中的max_execution_time。尝试更改为30以上。

http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time

较慢(较低带宽)的连接可能需要更长时间才能上传较大的图像,从而导致错误输出,您的连接可能会更快。