簡體   English   中英

使用ajax和php上傳圖像失敗(無錯誤日志)

[英]Image upload with ajax and php is failing (no error log)

由於某些原因,以下我用於上傳圖片的代碼失敗了...這是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();
});

肯定知道的是:

1ºphp腳本已正確實現,因為我故意使部分代碼失敗,並收到有關內部php錯誤的錯誤消息。

查詢正確完成(或至少按其語法)。

與#image相關的javascript函數也起作用。

我只想上傳用戶選擇的一張圖像(即使他選擇了100個其他項目)。 但是正如我所說,我什至在日志中都沒有收到錯誤消息...對此有什么想法嗎? 非常感謝你!


編輯

我更改了代碼

$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>';

由於某種原因,它現在停止在if(move_uploaded_file($_FILES['image']['tmp_name'], $newname)) 我已經var_dump'd這確實是“假”,但我不明白為什么。 這是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) } }

編輯2

警告:move_uploaded_file(profile / uploads / 1388794617.png):無法打開流:第37行的profile / uploadProfilePicture.php中沒有此類文件或目錄警告:move_uploaded_file():無法將'/ tmp / phppFfoL4'移至'profile /uploads/1388794617.png”(位於第37行的profile / uploadProfilePicture.php中)

我應該如何指定$ uploaddir甚至$ newname?

編輯

這就是我用的。 注意注釋掉的條件語句。

<?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>'; 
    } 

// }

原始答案

好的,我發現了問題。 刪除PHP中的所有[0] ,它現在可以工作。 它們用於數組,並且由於您僅將其用於單個文件上傳,因此失敗了。

旁注:您可能要在$uploaddir = "profile/uploads";的末尾添加/ $uploaddir = "profile/uploads"; $uploaddir = "profile/uploads/";

以下沒有[0] ,並已將其測試為沒有JS的純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']);

    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>'; 
    } 

} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM