Jump to content

Getting an images URL when uploading (for storing in SQL)


etymole

Recommended Posts

Hi again

 

I'm having a little trouble with this as I'm working from a book example and trying to get the URL from the image I have uploaded so that I can store it in my sql table (to be referenced and viewed later)

 

Here is my code:

 

<?php # upload_image.php

// Check if the form has been submitted.
if (isset($_POST['submitted'])) {

// Check for an uploaded file.
if (isset($_FILES['upload'])) {

	// Validate the type. Should be jpeg, jpg, or gif.
	$allowed = array ('image/gif', 'image/jpeg', 'image/jpg', 'image/pjpeg');
	if (in_array($_FILES['upload']['type'], $allowed)) {

		// Move the file over.
		if (move_uploaded_file($_FILES['upload']['tmp_name'], "uploads/{$_FILES['upload']['name']}")) {

			echo '<p>The file has been uploaded!</p>';

		} else { // Couldn't move the file over.

			echo '<p><font color="red">The file could not be uploaded because: </b>';

			// Print a message based upon the error.
			switch ($_FILES['upload']['error']) {
				case 1:
					print 'The file exceeds the upload_max_filesize setting in php.ini.';
					break;
				case 2:
					print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
					break;
				case 3:
					print 'The file was only partially uploaded.';
					break;
				case 4:
					print 'No file was uploaded.';
					break;
				case 6:
					print 'No temporary folder was available.';
					break;
				default:
					print 'A system error occurred.';
					break;
			} // End of switch.

			print '</b></font></p>';

		} // End of move... IF.

	} else { // Invalid type.
		echo '<p><font color="red">Please upload a JPEG or GIF image.</font></p>';
		unlink ($_FILES['upload']['tmp_name']); // Delete the file.
	}

} else { // No file uploaded.
	echo '<p><font color="red">Please upload a JPEG or GIF image smaller than 512KB.</font></p>';
}

} // End of the submitted conditional.





require_once ('../mysql_connect.php'); // Connect to the db.

mysql_query("UPDATE users SET profile_pic= '$_FILES[upload]' WHERE user_id = '19'");

mysql_close(); // Close the database connection.




?> 




<form enctype="multipart/form-data" action="upload_image.php" method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="524288">

<fieldset><legend>Select a JPEG or GIF image to be uploaded:</legend>

<p><b>File:</b> <input type="file" name="upload" /></p>

</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>

 

 

The upload facility works fine, and I will eventually put this code into a users profile page to upload images linked only to their username. If I could get this working.

 

As you can see I have tried an SQL query but I'm not sure how to get the name:

 

mysql_query("UPDATE users SET profile_pic= '$_FILES[upload]' WHERE user_id = '19'");

 

I just want the name of the address the image was stored at....I'm so confused...

mysql_query("UPDATE users SET profile_pic= '$_FILES[upload]' WHERE user_id = '19'");

 

you've got to use single quotes for an associative key such as upload...  i.e $_FILES['upload']

but the problem in using single quotes would be that you would get an error at [ in your SQL syntax because there's just too many single quotes for PHP to interpret.  In order to fix that you can either:

 

Surround the variable in curly braces

mysql_query("UPDATE users SET profile_pic= '{$_FILES['upload']}' WHERE user_id = '19'");

Concatenate it in there

mysql_query("UPDATE users SET profile_pic= '" . $_FILES['upload'] . "' WHERE user_id = '19'");

or put your data in a variable that works with the way you normally did

$fileUpload = $_FILES[upload];
mysql_query("UPDATE users SET profile_pic= '$fileUpload' WHERE user_id = '19'");

 

But I can guarantee you'll still get an error because $_FILES['upload'] will be an array... with more information than what you're wanting. 

To see what's inside it use this code

echo "", print_r($_FILES) , "";

I can guarantee you'll still get an error because $_FILES['upload'] will be an array... with more information than what you're wanting. 

To see what's inside it use this code

echo "", print_r($_FILES) , "";

 

Inside a Files array will be something like

name - the name of the file

tmp_name - the path & name of the uploaded file on the server

size? - the size of the file in bytes

.... there's more but.. the point is

 

to access one of them you simply do this

$_FILES['upload']['name']  /// 

$_FILES['upload']['tmp_name']

$_FILES['upload']['size']

Thank you Zanus, I think I'm getting it.

However, now I try it and the value put into my SQL table is Array[tmp_name]. Like its not actually putting the value in.

 

I'm using - '$_FILES[upload][tmp_name]'

 

I know this isn't correct, but doing it your way gives me this parse error - Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

 

What am I doing wrong?

 

 

unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

Scrap my last reply, I just re-read your first post. Using the [name] inside the array I can get the file name.

Now all I have to do is find a way to call the full address when I access it from a specific users page.

 

Thanks alot mate!

Archived

This topic is now archived and is closed to further replies.

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