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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算