dsq30861 2012-04-17 02:04
浏览 20
已采纳

图片上传无效

I would really like your help. I would consider myself an intermediate PHP programmer, but I have never used file uploads before. I have been stuck on this problem for a long time. This is a simplified version of my code and I'm 99% sure the error lies somewhere in here. The output is always "The file wasn't an image file."

This is my HTML...

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" id ="partyPic"><br/>
<button  type="button" onClick="uploadFile()">upload</button>
</form>

This is my PHP...

$image = $_FILES['image']['tmp_name'];

if (!isset($image)){

  //Create default image.

}else{

$image =  mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
$name = mysql_real_escape_string($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
}

if($image_size == FALSE){
echo 'The file wasn\'t an image file.';
}else{
//I have code that successfully uploads stuff to my database.
}

If you could help it would be greatly appreciated.

Thank you, Rick Ryan

  • 写回答

3条回答 默认 最新

  • donglang2010 2012-04-17 02:33
    关注

    Example Upload from http://www.php.net/manual/en/features.file-upload.post-method.php:

    The basic Form:

    <!-- The data encoding type, enctype, MUST be specified as below -->
    <form enctype="multipart/form-data" action="__URL__" method="POST">
        <!-- MAX_FILE_SIZE must precede the file input field -->
        <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
        <!-- Name of input element determines name in $_FILES array -->
        Send this file: <input name="userfile" type="file" />
        <input type="submit" value="Send File" />
    </form> 
    

    The PHP:

    <?php
    
    $uploaddir = '/var/www/uploads/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
    
    echo '<pre>';
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "File is valid, and was successfully uploaded.
    ";
    } else {
        echo "Possible file upload attack!
    ";
    }
    
    echo 'Here is some more debugging info:';
    print_r($_FILES);
    
    print "</pre>";
    
    ?> 
    

    The example from the manual.

    So you should build apon something like this:

    <form enctype="multipart/form-data" action="upload.php" method="POST">
        <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
        Send this file: <input name="userfile" type="file" />
        <input type="submit" value="Send File" />
    </form> 
    
    <?php
    if($_SERVER['REQUEST_METHOD'] == 'POST' && $_FILES['userfile']['error'] == 'UPLOAD_ERR_OK'){
        $uploaddir = '/var/www/uploads/';
        $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
        list($width, $height, $type, $attr) = getimagesize($_FILES['userfile']['tmp_name']);
    
        if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
            echo "File was successfully uploaded.
    ";
            ... Do Database stuff
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作