AbhimanuSharma
AbhimanuSharma

Reputation: 453

Error in image upload using PHP GD Library

I am not an PHP expert but i was trying to upload an image file using GD library but my code is showing error. Then i tried to echo the output but it is not showing the temporary location of the image file.

This is my code:

if(isset($_REQUEST['upload_btn'])){
$filename = $_FILES['file_upload']['name'];
list($w,$h)= getimagesize($filename);
$imgString = $_FILES['file_upload']['tmp_name'];
echo "Temp = $imgString <br>";
echo "Name = $filename <br>";
echo "Width = $w <br>";
echo "Height = $h";
}

Here is my HTML code:

<form enctype="multipart/form-data" method="post" target="_self" >
<input type="file" id="upload" name="file_upload" />
<input type="submit" value="upload" name="upload_btn" />
</form>

The output of the above code is as follows:

Temp =
Name = image1.jpg
Width = 4440
Height = 3294

Please tell me what i am doing wrong?

Upvotes: 1

Views: 708

Answers (1)

Tintu C Raju
Tintu C Raju

Reputation: 2700

$_FILES['file_upload']['name'] is just the name of the file. Which is actually not avaible on the server. unless you move the temp_file with the help of move_uploaded_file() method. You have to use $_FILES['tmp_name'] which is actually the uploaded file.

    <?php 
    if(isset($_REQUEST['upload_btn'])){
    $filename = $_FILES['file_upload']['name'];
    $imgString = $_FILES['file_upload']['tmp_name'];
    list($w,$h)= getimagesize($imgString);

    echo "Temp = $imgString <br>";
    echo "Name = $filename <br>";
    echo "Width = $w <br>";
    echo "Height = $h";
    }
    ?>

This was the case only if the file upload was successful. If the upload is not successful it will raise an error and it will be an error number which can be accessed from

$_FILES['file_upload']['error']

If it is 0 then your upload was successful. Otherwise the upload is failed. Then you will not get a result in $_FILES['file_upload']['tmp_name']

Upvotes: 2

Related Questions