Question

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 ?

Was it helpful?

Solution

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.

OTHER TIPS

The Error Messages Explained manual page leaves little doubt:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top