Page 1 of 1

[solved, thanks feyd] Image upload wont work

Posted: Sat Jun 09, 2007 4:52 pm
by .Stealth
hello, im quite stuck.
im new to uploading files and ive just created this script to upload images to a folder called tut_images.
once it has been added, i will put the filename into a sql database ready for calling in future.
i was wondering if anybody could spot the problem, thanks.


heres the script, it says the image has been added but it has not, the folder is chmod'ed to 777.

Code: Select all

if(isset($_POST['name'])){//check to see if the user has added one
		
		//handle tutorial image first
		if(isset($_FILES['tut_image'])){//check to see if an image has been uploaded
		
			//make sure its a gif or jpeg
			if(eregi('^image/p?jpeg(;.*)?$', $_FILES['tut_image']['type'])
			or eregi('^image/gif(;.*)?$', $_FILES['tut_image']['type'])){
				if(eregi('^image/p?jpeg(;.*)?$', $_FILES['tut_image']['type'])){
					$extenstion = '.jpg';
				}
				else{
					$extenstion = '.gif';
				}
				$file		= time() . $_SERVER['REMOTE_ADDR'] . $extenstion;//prepare file name
				$filename 	= $_SERVER['DOCUMENT_ROOT'] . "/tut_images/" . $file;//prepare file name and directory
				
				if(!is_uploaded_file($_FILES['tut_image']['tmp_name']) && copy($_FILES['tut_image']['tmp_name'], $filename)){
					exit('p>Unable to add image</p>');
				}
				else{
					echo "<p>Image Added!</p>";
				}
			}
			else{
				exit('Only .jpg and .gif file extensions allowed');
			}
		}
		exit();

ive exited early just while testing.

heres the form that does the magic:

Code: Select all

<form name="add_tut" method="post" enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF']; ?>">
	  			<label>Tutorial Name:<br />
	  				<input type="text" name="name" />
	  			</label><br /><br />
	  			
	  			<label>Tutorial Image:<br />
    				<input type="file" name="tut_image" />
    			</label><br /><br />
	  
	  			<label>Description:<br />
	  				<input type="text" name="description" />
	  			</label><br /><br />
	  
	  			<label>Keywords:<br />
	  				<input type="text" name="keywords" />
	  			</label><br /><br />
	  			<p>In Category's:</p>

	<?php
						//get tutorial categorys
				$sql 			= @mysql_query("SELECT * FROM tutorial_categorys;");
		
				if(!$sql){
						exit("unable to retreive tutorial categorys");
				}
				
				while($cat 		= mysql_fetch_array($sql)){
					$cat_id	 	= $cat['id'];
					$cat_name	= $cat['name'];
					
					echo "<label><input type=\"checkbox\" name=\"cats[]\" value=\"$cat_id\" />$cat_name</label><br /><br />";
					}
					
				$oFCKeditor = new FCKeditor('content');
				$oFCKeditor->BasePath = '../fckeditor/';
				$oFCKeditor->Value = "";
				$oFCKeditor->Width  = '490' ;
				$oFCKeditor->Height = '500' ;
				$oFCKeditor->Create();
				
	?>
				<br />
				<input type="submit" value="Save Tutorial">
				</form>

thanks for any help :)

Posted: Sat Jun 09, 2007 5:39 pm
by feyd
Symptoms of this "problem" are...?

Posted: Sat Jun 09, 2007 5:42 pm
by .Stealth
everything happens how it is supposed to except the image going into the folder i.e it returns with a confirmation that it all worked, but the image isn't in the folder specified.

i just cant see why its not working, it all looks fine to me.

was just wondering if any more experienced programmers would be able to spot the problem.

thanks

Posted: Sat Jun 09, 2007 5:44 pm
by feyd
Have you added some poor-man's debugging? (echoes in various locations.)

Posted: Sat Jun 09, 2007 5:49 pm
by .Stealth
feyd wrote:Have you added some poor-man's debugging? (echoes in various locations.)
why i think i did :D

i have swapped this bit:

Code: Select all

if(!is_uploaded_file($_FILES['tut_image']['tmp_name']) && copy($_FILES['tut_image']['tmp_name'], $filename)){
                                        exit('p>Unable to add image</p>');
                                }
                                else{
                                        echo "<p>Image Added!</p>";
                                }

to this:

Code: Select all

if(is_uploaded_file($_FILES['tut_image']['tmp_name']) && copy($_FILES['tut_image']['tmp_name'], $filename)){
					echo '<p>Image Added!</p>';
				}
				else{
					echo "<p>Unable to add image</p>";
				}
			}
			else{
				exit('Only .jpg and .gif file extensions allowed');
			}

thankyou for your unique help feyd.
made me think instead of getting the actual answer :D