duanruoyu6675 2014-01-03 22:55
浏览 16
已采纳

使用ajax和php的图像上传失败(没有错误日志)

The following code I'm using to upload images is failing for some reason... Here is the HTML

<form id="image_upload" enctype="multipart/form-data" action="uploadImage.php" method="post" name="prof_picture">
    <input id="image1" style="display:none;" name="image" accept="image/jpeg" type="file">
    <input id="image2" value="Submit" type="submit" style="display:none;">
</form>

PHP (uploadImage.php)

include('../sqlconnection.php');
define ("MAX_SIZE","1000");

function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

$valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") 
{
    $uploaddir = "profile/uploads"; //Image upload directory

    $filename = stripslashes($_FILES['image']['name'][0]);

    echo $filename;

    $size=filesize($_FILES['image']['tmp_name'][0]);

    echo $filename;
    //Convert extension into a lower case format
    $ext = getExtension($filename);
    $ext = strtolower($ext);
    //File extension check
    if(in_array($ext,$valid_formats))
    {
    //File size check
    if ($size < (MAX_SIZE*1024))
    { 
    $image_name=time().$filename; 
    echo "<img src='".$uploaddir.$image_name."' class='imgList'>"; 
    $newname=$uploaddir.$image_name; 
    //Moving file to uploads folder
    if (move_uploaded_file($_FILES['image']['tmp_name'][0], $newname)) 
    { 
    $time=time(); 
    //Insert upload image files names into user_uploads table
    mysql_query("UPDATE table SET image='$image_name' WHERE id='$user_id'");
    }
    else 
    { 
    echo '<span class="imgList">failed</span>'; } 
    }

    else 
    { 
    echo '<span class="imgList">failed</span>'; 
    } 

    } 

    else 
    { 
    echo '<span class="imgList">failed</span>'; 
    } 

} 

JS

$('#image1').on('change', function() {
        $("#image").attr('src',"profile/loading.gif");

        $("#image_upload").ajaxForm({
                        target: '#image'
        }).submit();
});

What I know for sure:

The php script is being achieved correctly because I failed part of the code on purpose and attained an error message regarding an internal php error.

The query is being done correctly (or at least by its syntax).

The javascript function related to #image is also working.

I only want to upload one image that the user selects (even if he selects 100 other items). But as I said, I don't even get an error message on the log... Any ideas on this one? Thank you very much!


EDIT

I've changed the code a bit

$ext = strtolower($ext);
    if(in_array($ext,$valid_formats)){
        if ($size < (MAX_SIZE*1024)){ 
            $image_name=time().$user_id."--pfi-".$filename;
            $newname=$uploaddir.$image_name;
                if (move_uploaded_file($_FILES['image']['tmp_name'], $newname)){
                    mysql_query("UPDATE table SET image='$image_name' WHERE id='$user_id'");
                }else echo '<span class="imgList">This message appears </span>'; 
        }else echo '<span class="imgList">You have exceeded the size limit!</span>';
    }else echo '<span class="imgList">Unknown extension!</span>';

For some reason it now stops at if(move_uploaded_file($_FILES['image']['tmp_name'], $newname)). I've var_dump'ed this and it is indeed "false" but I can't get to understand why. Here is var_dump($_FILES):

array(1) { ["image"]=> array(5) { ["name"]=> string(21) "060424_hubble_big.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(14) "/tmp/phpZYaDkm" ["error"]=> int(0) ["size"]=> int(35641) } }

EDIT 2

Warning: move_uploaded_file(profile/uploads/1388794617.png): failed to open stream: No such file or directory in profile/uploadProfilePicture.php on line 37 Warning: move_uploaded_file(): Unable to move '/tmp/phppFfoL4' to 'profile/uploads/1388794617.png' in profile/uploadProfilePicture.php on line 37

how should I specify $uploaddir or even $newname?

  • 写回答

1条回答 默认 最新

  • dongtan7351 2014-01-03 23:39
    关注

    Edit

    This is what I used. Notice the commented out conditional statements.

    <?php
    
    // include('../sqlconnection.php');
     define ("MAX_SIZE","1000000000");
    
    
    function getExtension($str)
    {
    $i = strrpos($str,".");
    if (!$i) { return ""; }
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
    }
    
    $valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
    // if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") 
    // {
        $uploaddir = "profile/uploads/"; //Image upload directory
    
        $filename = stripslashes($_FILES['image']['name']);
    
        echo $filename;
    
        $size=filesize($_FILES['image']['tmp_name']);
    
        echo $filename;
        //Convert extension into a lower case format
        $ext = getExtension($filename);
        $ext = strtolower($ext);
        //File extension check
        if(in_array($ext,$valid_formats))
        {
        //File size check
        if ($size < (MAX_SIZE*1024))
        { 
        $image_name=time().$filename; 
        echo "<img src='".$uploaddir.$image_name."' class='imgList'>"; 
        $newname=$uploaddir.$image_name; 
        //Moving file to uploads folder
        if (move_uploaded_file($_FILES['image']['tmp_name'], $newname)) 
        { 
        $time=time(); 
        //Insert upload image files names into user_uploads table
    //    mysql_query("UPDATE table SET image='$image_name' WHERE id='$user_id'");
        }
        else 
        { 
        echo '<span class="imgList">failed</span>'; } 
        }
    
        else 
        { 
        echo '<span class="imgList">failed</span>'; 
        } 
    
        } 
    
        else 
        { 
        echo '<span class="imgList">failed</span>'; 
        } 
    
    // }
    

    Original answer

    Ok, I found the problem. Remove all [0] in your PHP and it will now work. Those are used for arrays and since you're only using it for single file uploads, is why it failed.

    Sidenote: You may want to add a / at the end of $uploaddir = "profile/uploads"; as in $uploaddir = "profile/uploads/";

    The following doesn't have the [0]'s and have tested it as pure PHP with no JS.

    include('../sqlconnection.php');
    define ("MAX_SIZE","1000");
    
    function getExtension($str)
    {
    $i = strrpos($str,".");
    if (!$i) { return ""; }
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
    }
    
    $valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") 
    {
        $uploaddir = "profile/uploads"; //Image upload directory
    
        $filename = stripslashes($_FILES['image']['name']);
    
        echo $filename;
    
        $size=filesize($_FILES['image']['tmp_name']);
    
        echo $filename;
        //Convert extension into a lower case format
        $ext = getExtension($filename);
        $ext = strtolower($ext);
        //File extension check
        if(in_array($ext,$valid_formats))
        {
        //File size check
        if ($size < (MAX_SIZE*1024))
        { 
        $image_name=time().$filename; 
        echo "<img src='".$uploaddir.$image_name."' class='imgList'>"; 
        $newname=$uploaddir.$image_name; 
        //Moving file to uploads folder
        if (move_uploaded_file($_FILES['image']['tmp_name'], $newname)) 
        { 
        $time=time(); 
        //Insert upload image files names into user_uploads table
        mysql_query("UPDATE table SET image='$image_name' WHERE id='$user_id'");
        }
        else 
        { 
        echo '<span class="imgList">failed</span>'; } 
        }
    
        else 
        { 
        echo '<span class="imgList">failed</span>'; 
        } 
    
        } 
    
        else 
        { 
        echo '<span class="imgList">failed</span>'; 
        } 
    
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)