doumao9363 2014-05-06 11:43
浏览 142
已采纳

上传时没有获取文件类型和tmp_name

I want to upload image and pdf same time but it is giving error. I don't know what I have missed, image is uploading properly but there is problem while uploading a pdf. Here is the piece of code.

<pre>
<?php
if ($_GET["action"] == "submit") {

    $imageType="press_release_image_";
    $ext= substr(basename($_FILES['brwAddNews']['name']),-4);
    $filename=$imageType.$ext; 
    $target_path = $filename;

    $pdfType="press_release_pdf_";
    $pdfext= substr(basename($_FILES['brwAddNewsPdf']['name']),-4);
    $pdffilename=$pdfType.$pdfext; 
    $pdf_target_path = $pdffilename; 
    print_r($_FILES) ;
    $pdfext= substr(basename($_FILES['brwAddNewsPdf']['name']),-4);
    if($pdfext==".pdf"){
        if(move_uploaded_file($_FILES['brwAddNews']['tmp_name'], $target_path) && move_uploaded_file($_FILES['brwAddNewsPdf']['tmp_name'], $pdf_target_path))
        {
            echo "file uploaded";
        }
        else echo "Not uploaded";
    }
    else echo "Its not a pdf file";
}

?>

<center>
<form action="try.php?action=submit" method="post" enctype="multipart/form-data" name="frmNews"  id="frmNews" >
    <table><tr><td>NAME :<td/><td><input name="news_name" type="text" class="textfield" id="news_name" value="" /><br></td></tr>
    <tr><td>PDF : <td/><td><input name="brwAddNewsPdf" type="file" id="brwAddNewsPdf" multiple="multiple" /><br></td></tr>
    <tr><td>IMAGE : <td/><td><input name="brwAddNews" type="file" id="brwAddNews" multiple="multiple"/><br></td></tr>
    <tr><td><td/><td><input name="btnSave" type="submit" class="buttons" id="btnSave"  value="Save"></td></tr>
</form>
</center>

I am getting output as :

Array
(
    [brwAddNewsPdf] => Array
        (
            [name] => Newsletter-volume16.pdf
            [type] => 
            [tmp_name] => 
            [error] => 1
            [size] => 0
        )

    [brwAddNews] => Array
        (
            [name] => PDF-thumbnail.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/php0KuLSj
            [error] => 0
            [size] => 4536
        )

)
Not uploaded

What may be the problem ? What i have missed ?

  • 写回答

2条回答 默认 最新

  • douxunwei8259 2014-12-03 14:55
    关注

    I was with the same error until now. As I saw this question with that poor answer I needed to help.

    It is a little bit old question but here the solution:

    First thing, the error. Your output ways that you have 2 files, the first with Error 1, and the second with Error 0. In the documentation we have this explanation about the error value:

    UPLOAD_ERR_OK
    Value: 0;
    There is no error, the file uploaded with success.

    UPLOAD_ERR_INI_SIZE
    Value: 1;
    The uploaded file exceeds the upload_max_filesize directive in php.ini.

    Second, knowing that your error is about the size of your archive, you will need just to increase the upload_max_filesize on your php.ini file. It's a little bit simple, but you can do better. If you put this on your upload-form:

    <input type="hidden" name="MAX_FILE_SIZE" value="12000" />
    

    You will check the size of the file BEFORE the upload. It's not best thing to control the max size of the upload(it can be cheated simply), but it will help your user to know that his file is too large, without needing to upload it.
    See here more informations about PHP Upload.

    Finnaly, restart your php. Here I use php5-fpm.

    # service php5-fpm restart
    

    Hint: Don't use the exact size that you want to upload in the php.ini, I use some MBs more than that I want. Then I treat it in my code. This will remove the error and you can treat as you wish in your application. ex: if you have files with 12M, put 20M.

    OBS.: With multiple files, I don't really know if sum size or not.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥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-桌布的计算