Jump to content

image file not uploading to directory


silverglade

Recommended Posts

Hi, I am storing image names in the database, and the image in the server directory "image". I am using time() to rename the images.

 

Basically what I do is, check the images, rename them, upload to server (or supposed to), and insert the name of the image into the database, then I use the database image name in an img src, to output the uploaded image.

 

Here is the code. Basically it looks like the images are not uploading to the "images" directory on the server.

 

Any help GREATLY appreciated. thanks.

 

here is the upload code. to put image in the server renamed, and then under that the image display code taken from the database.

 

<?php if(isset($_POST['photoUpload']))

{


	//reads the name of the file the user submitted for uploading
	$image=$_FILES['image']['name'];
	//if it is not empty
		if ($image)
		{
		//get the original name of the file from the clients machine
		$filename = stripslashes($_FILES['image']['name']);
		//get the extension of the file in a lower case format
		$extension = getExtension($filename);
		$extension = strtolower($extension);
		//if it is not a known extension, we will suppose it is an error and will not upload the file,
		//otherwise we will do more tests
			if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
			{
			//print error message
			echo '<h1>Unknown extension!</h1>';
			$errors=1;
			}
		}
	//end if here
//was else here

	//get the size of the image in bytes
	//$_FILES['image']['tmp_name'] is the temporary filename of the file
	//in which the uploaded file was stored on the server
	$size=filesize($_FILES['image']['tmp_name']);

	//compare the size with the maxim size we defined and print error if bigger
		if ($size > MAX_SIZE*1024)
		{
		echo '<h1>You have exceeded the size limit!</h1>';
		$errors=1;
		}

		//we will give an unique name, for example the time in unix time format

			$image_name=time().'.'.$extension;
			//the new name will be containing the full path where will be stored (images folder)
			$newname=$image_name;
			//we verify if the image has been uploaded, and print error instead
			$copied = copy($_FILES['image']['tmp_name'], $newname);

				if (!$copied)
				{
				echo '<h1>Copy unsuccessful!</h1>';
				$errors=1;
				}
				else{

				mysqlconnect();

				// update the photo name in the database
				$result = mysql_query("UPDATE users SET profilePhoto='$newname'  WHERE                          			                      username='$currentUser'") 
					or die(mysql_error());  

				echo "<h1>Photo Update Successful!</h1>";
				}

				//replace and upload pic name to the database for later output from database.


				//echo out all images
					/*	$dir = "images/";


						if (is_dir($dir))
						 {
							if ($dh = opendir($dir))
							 {
								while (($file = readdir($dh)) !== false) 
								{

								if($file == ".")
								continue;
								if($file == "..")
								continue;

								echo "<img src='{$dir}{$file}' alt='{$file}' height='200' width='200' />";
									 }
								closedir($dh);
							}
						}

						*/
							mysqlconnect();



								// SELECT FOR THE IMAGE

								$mysql = "SELECT profilePhoto FROM users WHERE 				                             							                                      username ='$currentUser'";


						$result = mysql_query($mysql);

						$dir = "images/";
						// if there is a result, echo out the pic using the new name of the pic

						if(result)
						{
						   echo "<img src='{$dir}{$newname}' alt='{$newname}' height='200' width='200' />";
						   
						   }
						   
						if (!$result) {
							echo "Could not successfully run query ($sql) from DB: " . mysql_error();
							exit;
						}

						if (mysql_num_rows($result) == 0) {
							echo "No rows found, nothing to print so am exiting";
							exit;

}//isset photo if ?>

 

 

Link to comment
Share on other sites

thank you very much for that. I used error reporting and it caught if(result) should be if($result). which would be the reason why the images did not show. But they don't show up still.

 

I used the mysqlconnect(); to indicate to the viewers of the post that that is where I connected to the database at that point, because when I did not paste in the database connecting information at those spots, I got error messages, so I started pasting in the database connection information right in the spots where I posted "mysqlconnect();" in this post.

 

 

the images are not uploading to the "images" directory on the server, and I think move_uploaded_file is  to move existing files on the server. I don't think there is move_uploaded_photo? thank you for helping me to this point. :D

Link to comment
Share on other sites

awesome thank you. I FIXED IT!! hehe. Does anyone know how I can replace a user profile image and have it refresh to the new image as soon as I submit the form? The image is not refreshing, I have to reload the whole page for the new profile image to be seen.

 

here is the code I am using to do the image replacement.

 

	
				if(file_exists("images/".$currentUser.".jpg")){
   						 unlink("images/".$currentUser.".jpg");
    						clearstatcache(); 

					}

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.