Jump to content

image uploader, uploads but still gives error


liamdawe

Recommended Posts

Basically my image upload seems to work fine, it uploads the image in question ive tested that a few times, but it seems to then return with a blank error message.

 

Here is my code:

function upload_item($item_id)
{
    global $site_config, $db_pxs, $lang, $pxs, $error;
    
    $error = '';
    
    // first we check if it is allowed
    $allowed_extensions = array('image/jpeg');
            
    if (in_array($_FILES['new_upload']['type'], $allowed_extensions))
    {
        // check file size not bigger than 1.47mb
        if ($_FILES['new_upload']['size'] > 1542000)
        {
            $error = 'Sorry file is too big!';
            return false;    
        }
    
        // THE MAIN FILE
        // give the file a random file name
        $filename = rand() . 'id' . $item_id . $_FILES['new_upload']['name'];
                
        // the actual file
        $source = $_FILES['new_upload']['tmp_name'];
                
        // where to upload to
        $target = "uploads/" . $filename;
        //
                
        if (move_uploaded_file($source, $target))
        {
            return $filename;
        }
            
        else
        {
            $error = 'Could not upload file, please contact the admin!';
            return false;    
        }
    }
    
    else
    {
        $error = 'Wrong file type, allowed to upload .jpg files only.';
        return false;    
    }
}

The image appears in my upload directory fine.

I don't see why it gives a blank error, i set only 3 error messages possible?

Here is the checking code:

    // initial screenshot
    if ($_FILES['new_upload']['error'] == UPLOAD_ERR_OK)
    {
        if (upload_item($db_pxs->last_id()) == false)
        {
            $pxs->admin_error($error);
        }

        $screenshot_sql = "INSERT INTO `item_screenshots` SET `item_id` = {$db_pxs->last_id()}, `image` = '{$filename}'";
        $query_shot = $db_pxs->query($screenshot_sql);
    }

It doesn't seem to get to the screenshot sql, giving a blank error message above , yet it does upload the file :S

 

Any help would be great!

Is the code in your second block in a function also?  If so, you need to make sure that $error is set as global there as well.  Besides that, you should try and echo some troubleshooting text at different stages of your upload_item function to try and figure out where it's dying.

It returns a blank error message because there is no error and you initialized it as blank. "$error='';"  instead set it to NULL and check to see if it is set.

<?php 
$error=NULL;
if(isset($error)) {
//We have an error so print it and do something.
}
?>

I found the error, i changed the uploader code to this:

function upload_item($item_id)

{

global $site_config, $db_pxs, $lang, $pxs, $error, $filename;



$error = '';



// first we check if it is allowed

$allowed_extensions = array('image/jpeg');



if (in_array($_FILES['new_upload']['type'], $allowed_extensions))

{

	// check file size not bigger than 1.47mb

	if ($_FILES['new_upload']['size'] > 1542000)

	{

		$error = 'Sorry file is too big!';

		return false;	

	}



	// THE MAIN FILE

	// give the file a random file name

	$filename = rand() . 'id' . $item_id . $_FILES['new_upload']['name'];



	// the actual file

	$source = $_FILES['new_upload']['tmp_name'];



	// where to upload to

	$target = "uploads/" . $filename;

	//



	if (move_uploaded_file($source, $target))

	{
		return true;

	}



	else

	{

		$error = 'Could not upload file, please contact the admin!';

		return false;	

	}

}



else

{

	$error = 'Wrong file type, allowed to upload .jpg files only.';

	return false;	

}

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.