Upload Function Issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jzmwebdevelopment
Forum Commoner
Posts: 32
Joined: Mon Nov 01, 2010 1:45 pm

Upload Function Issue

Post by jzmwebdevelopment »

Hello,

I am having an issue with my upload function - I can get the image name etc to save to the database but I cannot get my image to be moved to the Gallery dir:

Code: Select all


	public function Upload($sName, $sNewFileName){
		
		$newname = dirname(__FILE__).$_SERVER['HTTP_HOST'];'Nyken/includes/images/Gallery/'.$sNewFileName;
		
		move_uploaded_file($_FILES[$sName]['tmp_name'],$newname);
    	}
    


Print_R

Array ( [name] => Corn.jpg [type] => image/jpeg [tmp_name] => /tmp/phpyKID4B [error] => 0 [size] => 72068 )

Any Ideas?
aswinlutchanah
Forum Newbie
Posts: 2
Joined: Wed Dec 22, 2010 5:12 pm

Re: Upload Function Issue

Post by aswinlutchanah »

Code: Select all

$newname = dirname(__FILE__).$_SERVER['HTTP_HOST'];'Nyken/includes/images/Gallery/'.$sNewFileName;
Hello,

I found your question on twitter and thought I'l tell you to change the ";" into a "." after $_SERVER['HTTP_HOST'].
jzmwebdevelopment
Forum Commoner
Posts: 32
Joined: Mon Nov 01, 2010 1:45 pm

Re: Upload Function Issue

Post by jzmwebdevelopment »

That has made no difference
aswinlutchanah
Forum Newbie
Posts: 2
Joined: Wed Dec 22, 2010 5:12 pm

Re: Upload Function Issue

Post by aswinlutchanah »

Code: Select all

move_uploaded_file($_FILES[$sName]['tmp_name'],$newname);
'$sName' should be replaced by something like this 'input_name' without the dollar sign.
jzmwebdevelopment
Forum Commoner
Posts: 32
Joined: Mon Nov 01, 2010 1:45 pm

Re: Upload Function Issue

Post by jzmwebdevelopment »

Right now I feel like a egg - I forgot to call the upload function.

My issue now is that I get the following:

Warning: Missing argument 1 for Image::imageUpload(), called in /home/devnoo/public_html/Nyken/Admin/addimage.php on line 33 and defined in /home/devnoo/public_html/Nyken/Admin/includes/class/class.image.php on line 123

Warning: Missing argument 2 for Image::imageUpload(), called in /home/devnoo/public_html/Nyken/Admin/addimage.php on line 33 and defined in /home/devnoo/public_html/Nyken/Admin/includes/class/class.image.php on line 123

This is my form:

Code: Select all


    if($formImageUpload->getValid() == true){
    
	$AddImage = new Image();
    
	$AddImage->setImageName($_POST["ImageName"]);
	$AddImage->setImagePath($_FILES["ImagePath"]["name"]);
	
	$AddImage->imageUpload();
	
	$AddImage->saveImage();
 
	    $Message = "Thank You, Your Image Has Been Uploaded";
    
    }else{
	
	$Message = "Sorry You Have An Error Please Try Again";
    }
    
}


$formImageUpload->openFieldset();
$formImageUpload->makeInputBox('Image','ImageName','CheckInput(this.id);');
$formImageUpload->makeHiddenField("MAX_FILE_SIZE", MAX_SIZE);
$formImageUpload->makeUpLoadBox('Image Path',"ImagePath");
$formImageUpload->makeSubmitButton("submit","Image Upload");
$formImageUpload->closeFieldset();

jzmwebdevelopment
Forum Commoner
Posts: 32
Joined: Mon Nov 01, 2010 1:45 pm

Re: Upload Function Issue

Post by jzmwebdevelopment »

I have set the above to:


$AddImage->imageUpload($_FILES["nqme"],$_POST["ImageName"]);


Still not working :(
ccsdg
Forum Newbie
Posts: 16
Joined: Thu Dec 23, 2010 1:55 am

Re: Upload Function Issue

Post by ccsdg »

Hey jzm :) coming here from twitter too.

I'm assuming your calling code now looks like:

Code: Select all

$AddImage->imageUpload($_FILES['name'],$_POST['ImageName']);
Assuming there are no other errors, you could try:

Code: Select all

print_r($_FILES);
print_r($_POST);
and see if they are empty at the respective positions. Also check spelling (caps etc) of 'name' and 'ImageName' as i noticed you posted 'nqme'.
jzmwebdevelopment
Forum Commoner
Posts: 32
Joined: Mon Nov 01, 2010 1:45 pm

Re: Upload Function Issue

Post by jzmwebdevelopment »

Thanks,

I fixed the spelling mistake but that did not solve my issue :(

$_FILES = Array ( [ImagePath] => Array ( [name] => Corn.jpg [type] => image/jpeg [tmp_name] => /tmp/phpf4MbTV [error] => 0 [size] => 72068 ) )

$_POST = Array ( [ImageName] => Corn [MAX_FILE_SIZE] => 1000000 [submit] => Image Upload )

I hope this helps.

Thanks for your help
ccsdg
Forum Newbie
Posts: 16
Joined: Thu Dec 23, 2010 1:55 am

Re: Upload Function Issue

Post by ccsdg »

print_r:
$_FILES = Array ( [ImagePath] => Array ( [name] => Corn.jpg [type] => image/jpeg [tmp_name] => /tmp/phpf4MbTV [error] => 0 [size] => 72068 ) )
Your files array contains an array 'ImagePath' in which 'name' is a key. To access 'name' you'd have to first access the inner array, then inside the inner array access the 'name'. Eg:

Code: Select all

$aImagePath = $_FILES['ImagePath'];
$name = $aImagePath['name'];
$AddImage->imageUpload($name,$_POST["ImageName"]);
The structure of nested arrays can be hard to keep track of, the <pre> tag can be really useful here! (the function I posted to your facebook :p)
jzmwebdevelopment
Forum Commoner
Posts: 32
Joined: Mon Nov 01, 2010 1:45 pm

Re: Upload Function Issue

Post by jzmwebdevelopment »

Could you please do an example due to confusion.
ccsdg
Forum Newbie
Posts: 16
Joined: Thu Dec 23, 2010 1:55 am

Re: Upload Function Issue

Post by ccsdg »

try:

Code: Select all

echo '<pre>';
print_r($_FILES);
echo '</pre>';
and see what that looks like. (EDIT: sorry, syntax, fixed now)

you should have something like
[syntax]array(
1st key of array => array2 (
1st key of array2 => value1
2nd key of array2 => value2
3rd key of array2 => value3
)
)[/syntax] Assuming that is what you see, that should demonstrate that you have an array inside your files array. you need to access the 2nd array inside your files array before you can access the first key of the 2nd array (ie the 'name') key. if you try to access $_FILES['name'], that key doesn't exist because you need to go a level deeper first.
jzmwebdevelopment
Forum Commoner
Posts: 32
Joined: Mon Nov 01, 2010 1:45 pm

Re: Upload Function Issue

Post by jzmwebdevelopment »

Thanks,

print_r($_FILES);

Code: Select all


Array
(
    [ImagePath] => Array
        (
            [name] => Corn.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/phpz8Emls
            [error] => 0
            [size] => 72068
        )

)

I had nothing from 'name' but this was given from $_FILES

so lost :(
ccsdg
Forum Newbie
Posts: 16
Joined: Thu Dec 23, 2010 1:55 am

Re: Upload Function Issue

Post by ccsdg »

look at the structure you just posted. do you see that there's an array inside the $_FILES array?
jzmwebdevelopment
Forum Commoner
Posts: 32
Joined: Mon Nov 01, 2010 1:45 pm

Re: Upload Function Issue

Post by jzmwebdevelopment »

Yup array = name type size etc
ccsdg
Forum Newbie
Posts: 16
Joined: Thu Dec 23, 2010 1:55 am

Re: Upload Function Issue

Post by ccsdg »

so have a look at:

Code: Select all

echo '<pre>',
echo '$_FILES: <br />';
print_r($_FILES);
echo '<br />ImagePath: <br />';
print_r($_FILES['ImagePath']);
echo '</pre>';
Only the ImagePath array should have 'name' as an item directly inside it.

This means you can't access the 'name' from the $_FILES array directly. $_FILES only contains one item, it's an array and it's called 'ImagePath'. There is no 'name' in $_FILES. How should you access 'name' instead?
Post Reply