etymole Posted March 24, 2010 Share Posted March 24, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/196314-getting-an-images-url-when-uploading-for-storing-in-sql/ Share on other sites More sharing options...
Zane Posted March 24, 2010 Share Posted March 24, 2010 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) , ""; Quote Link to comment https://forums.phpfreaks.com/topic/196314-getting-an-images-url-when-uploading-for-storing-in-sql/#findComment-1030867 Share on other sites More sharing options...
etymole Posted March 24, 2010 Author Share Posted March 24, 2010 Right. It seems to be putting something in there but when I check my sql table, it tells me it's an Array. Is there a way I can break it down to display the URL as text? Quote Link to comment https://forums.phpfreaks.com/topic/196314-getting-an-images-url-when-uploading-for-storing-in-sql/#findComment-1030889 Share on other sites More sharing options...
Zane Posted March 24, 2010 Share Posted March 24, 2010 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'] Quote Link to comment https://forums.phpfreaks.com/topic/196314-getting-an-images-url-when-uploading-for-storing-in-sql/#findComment-1031005 Share on other sites More sharing options...
etymole Posted March 24, 2010 Author Share Posted March 24, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/196314-getting-an-images-url-when-uploading-for-storing-in-sql/#findComment-1031023 Share on other sites More sharing options...
etymole Posted March 24, 2010 Author Share Posted March 24, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/196314-getting-an-images-url-when-uploading-for-storing-in-sql/#findComment-1031026 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.