PHP image download does not work

advertisements

I have the below code which is not uploading the image tried many thing but does not work. I can not figure it out what is the issue but log ago the same code was used to work.

HTML Form

<form method="post" action="" enctype="multipart/form-data" name="form1">
<input size="25" name="file" type="file" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10pt" class="box"/>
<input type="submit" value="Upload" name="submit"/>
</form>

The last try code is below:

$change="";
$abc="";

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

$errors=0;

if($_SERVER["REQUEST_METHOD"] == "POST"){
    $image =$_FILES["file"]["name"];
    $uploadedfile = $_FILES['file']['tmp_name'];

if ($image) {

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

        $extension = getExtension($filename);
        $extension = strtolower($extension);

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
            $change='<div class="msgdiv">Unknown Image extension </div> ';
            $errors=1;
}else{
$size=filesize($_FILES['file']['tmp_name']);

if ($size > MAX_SIZE*1024){
    $change='<div class="msgdiv">You have exceeded the size limit!</div> ';
    $errors=1;
}

if($extension=="jpg" || $extension=="jpeg" ){
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}else if($extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);

}else {
$src = imagecreatefromgif($uploadedfile);
}

echo $scr;

list($width,$height)=getimagesize($uploadedfile);

$newwidth=1990;
$newheight=1100;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

$imu    =   mysql_query("select max(id)+1 as id from menu_items");
$ints   =   mysql_fetch_array($imu);
$chk    =   $ints['id'];

$ids    =   $ints['id'];

$srids  =   $ints['showroom_id'];

$name = $_FILES["file"]["name"];
$ext = end((explode(".", $name))); # extra () to prevent notice

$ses_msr    =   $_SESSION['ses_member_id'];
$folderPath = "images/rest_menu_items";
mkdir($folderPath);

$filename = "images/rest_menu_items/".$ids.'.'.$ext;
$filename1 = "images/small". $_FILES['file']['name'];

imagejpeg($tmp,$filename,100);
mysql_query("
UPDATE menu_items
SET
    filename='".$ids.'.'.$ext."'
WHERE
    id='".$_GET['id']."'
");

imagedestroy($src);
imagedestroy($tmp);
}}

}


Fix these:

Row 6:

function getExtension($str){

Row 48:

echo $src;

Row 79:

mkdir($folderPath);

And make sure you have write permissions.

Also PHP error log would help a lot. You can debug your code if you put

error_reporting( E_ALL );

to beginning of file.

Also make sure that you have installed PHP GD.

Create file whre only content is.

<?php
phpinfo(INFO_MODULES);
?>

There should be "GD Supported Enabled" If it is not found you need to install it.

If it is present then check write permissions, file test.txt must be present. So create empty file test.txt to test this.

$filename = 'test.txt';
if (is_writable($filename)) {
    echo 'The file is writable';
} else {
    echo 'The file is not writable';
}

If these things is checked then following code will work.

Tested code that work. I removed queries and SESSION data because I cant debug them without having all code.

<?php
$change="";
$abc="";

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

$errors=0;

if($_SERVER["REQUEST_METHOD"] == "POST"){
    $image =$_FILES["file"]["name"];
    $uploadedfile = $_FILES['file']['tmp_name'];

if ($image) {

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

        $extension = getExtension($filename);
        $extension = strtolower($extension);

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
            $change='<div class="msgdiv">Unknown Image extension </div> ';
            $errors=1;
}else{
$size=filesize($_FILES['file']['tmp_name']);

if ($size > MAX_SIZE*1024){
    $change='<div class="msgdiv">You have exceeded the size limit!</div> ';
    $errors=1;
}

if($extension=="jpg" || $extension=="jpeg" ){
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}else if($extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);

}else {
$src = imagecreatefromgif($uploadedfile);
}

echo $src;

list($width,$height)=getimagesize($uploadedfile);

$newwidth=1990;
$newheight=1100;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

imagejpeg($tmp,"Test.jpg",100);

imagedestroy($src);
imagedestroy($tmp);
}}

}
?>