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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
}
?>

Link to comment
Share on other sites

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;	

}

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.