PHP:上传和重命名图片

时间:2016-02-17 09:35:22

标签: php forms upload

我使用脚本上传和重命名文件。 表格看起来像这样。

<form action="" enctype="multipart/form-data" method="post">
<input type="text" name="shortlink" placeholder="Shortlink"><br>
<input id="file" name="file" type="file" />
<input id="Submit" name="submit" type="submit" value="Submit" />

php上传器看起来像这样

if (isset($_POST['submit']))
{
    $filename = $_FILES["file"]["name"];
    $file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
    $file_ext = substr($filename, strripos($filename, '.')); // get file name
    $filesize = $_FILES["file"]["size"];
    $allowed_file_types = array('.doc','.docx','.jpg','.jpeg','.pdf');  

    if (in_array($file_ext,$allowed_file_types) && ($filesize < 20000000000000))
    {   
        // Rename file
        $newfilename = $_POST['shortlink'] . $file_ext;
        if (file_exists("upload/" . $newfilename))
        {
            // file already exists error
            echo "You have already uploaded this file.";
        }
        else
        {       
            move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename);
            echo "File uploaded successfully.";     
        }
    }
    elseif (empty($file_basename))
    {   
        // file selection error
        echo "Please select a file to upload.";
    } 
    elseif ($filesize > 20000000000000)
    {   
        // file size error
        echo "The file you are trying to upload is too large.";
    }
    else
    {
        // file type error
        echo "Only these file typs are allowed for upload: " . implode(', ',$allowed_file_types);
        unlink($_FILES["file"]["tmp_name"]);
    }
}

当我尝试上传简单的文本文件(.txt,.doc,.docx)时,它非常有效。但是,当我尝试上传任何.jpg图像时,它没有给出任何响应。表格只是清除,这就是全部。没有错误消息或任何东西。 谁能告诉我错误的位置?

3 个答案:

答案 0 :(得分:0)

试试这个希望这会起作用: -

&#13;
&#13;
if(isset($_POST['submit']))
{
$allowedSize=2;//MB
$allowedFileSize=$allowedSize*1024*1024;//Bytes
$allowedExt=array('pdf','doc','docx');
$extnsn=implode(',' , $allowedExt);
if($_FILES['file']['error']==0)
{
if($_FILES['file']['size']<=$allowedFileSize)
 {
 $p_image=$_FILES['file']['name'];
 
$p_image=date('ymdHis').'_'.uniqid().'_'.$p_image;
 
$fileInfoArray=pathinfo($p_image);
$fileExt = $fileInfoArray['extension'];
 
 if(in_array($fileExt,$allowedExt)) {
        $src = $_FILES['file']['tmp_name'];
        $dest = "uploads/legal-internship/$p_image";
		 $ismoved = move_uploaded_file($src,$dest);  
        
		  $_POST['image'] = $p_image;
          

        } else {
          echo "<br>Please upload file with valid extension ($extnsn).PLease Try Again ";
        }
      } else {
        echo "Please upload file with MAX $allowedSize MB";
      }
    } else {
      echo "Error while Uploading File Size Exceed to 2MB";
	  }
&#13;
&#13;
&#13;

答案 1 :(得分:0)

旁注:&#34; 20000000000000&#34; = 20太字节。看起来像是一张大图......

答案 2 :(得分:0)

知道了!

我的wamp服务器是限制。在这里找到了解决问题的指南。 http://rj45tech.com/increasing_php_memory_limit_and_file_upload_size_wamp