PHP ---为什么我不能用PHP上传图片?

时间:2010-12-07 07:10:46

标签: php html

代码

<?php
    $allowed_filetypes = array('.jpg','.gif','.bmp','.png');
    $max_filesize = 5242888;
    $upload_path = '/files';
    $filename =$_FILES['userfile']['name'];
    $ext = substr($filename,strpos($filename,'.'),strlen($filename)-1); //Get the     extension form the filename.

    if(!in_array($ext,$allowed_filetypes))
        die('the file you attempted to upload is not allowed.');

    if(filesize($_FILES['userfile']['size'])>$max_filesize)
        die('the file you attempted to upload is too large.');

    if(!is_writable($upload_path)) {
        die('you cannot upload to the specified directory,please CHMOD it to 777.');
    }

    if (move_uploaded_file( $_FILES['userfile']['tmp_name'],$upload_path.$filename))
    {
        echo 'you file upload successful.view the file <a      href=".$upload_path.$filename.title="your file">here</a>';
    }
    else{
        echo 'failed';
    }

当我上传JPEG图像时,它会显示错误“您尝试上传的文件不被允许。”。我的代码出了什么问题?

主要的HTML代码:

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="userfile" id="file"/>
    <button>upload</button>

4 个答案:

答案 0 :(得分:2)

请使用表格标签

 <form action="upload.php" method="post" enctype="multipart/form-data">

答案 1 :(得分:0)

尝试替换:

$ext = substr($filename,strpos($filename,'.'),strlen($filename)-1); 
//get the  extension form the filename

通过

$ext = substr($filename,strpos($filename,'.'),4);
//get the     extension form the filename 

答案 2 :(得分:0)

尝试使用提交按钮,&lt; input type =“submit”name =“upload”value =“upload”/&gt;。

答案 3 :(得分:0)

<form action="upload.php" method="post" enctype="multipart/form-data"> 
    <input type="file" name="userfile" id="file"/>
    <input type="submit" name="sendFile" value="Upload" />
</form>

应该有用。