Page 1 of 2

Upload Function Issue

Posted: Wed Dec 22, 2010 4:57 pm
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?

Re: Upload Function Issue

Posted: Wed Dec 22, 2010 5:16 pm
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'].

Re: Upload Function Issue

Posted: Wed Dec 22, 2010 5:28 pm
by jzmwebdevelopment
That has made no difference

Re: Upload Function Issue

Posted: Wed Dec 22, 2010 7:42 pm
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.

Re: Upload Function Issue

Posted: Wed Dec 22, 2010 8:05 pm
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();


Re: Upload Function Issue

Posted: Wed Dec 22, 2010 9:57 pm
by jzmwebdevelopment
I have set the above to:


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


Still not working :(

Re: Upload Function Issue

Posted: Thu Dec 23, 2010 2:08 am
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'.

Re: Upload Function Issue

Posted: Thu Dec 23, 2010 2:20 am
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

Re: Upload Function Issue

Posted: Thu Dec 23, 2010 3:55 am
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)

Re: Upload Function Issue

Posted: Thu Dec 23, 2010 12:27 pm
by jzmwebdevelopment
Could you please do an example due to confusion.

Re: Upload Function Issue

Posted: Thu Dec 23, 2010 12:49 pm
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.

Re: Upload Function Issue

Posted: Thu Dec 23, 2010 1:06 pm
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 :(

Re: Upload Function Issue

Posted: Thu Dec 23, 2010 1:15 pm
by ccsdg
look at the structure you just posted. do you see that there's an array inside the $_FILES array?

Re: Upload Function Issue

Posted: Thu Dec 23, 2010 1:42 pm
by jzmwebdevelopment
Yup array = name type size etc

Re: Upload Function Issue

Posted: Thu Dec 23, 2010 1:48 pm
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?