therelelogo Posted June 27, 2010 Share Posted June 27, 2010 Hi all, Okay, after searching for about a week non stop - i'm beginning to wonder if uploading a file via php to mysql or filesystem is even possible, or if it's just a myth. can anbody provide a link, code or other on how to upload a file (image) that is easy to understand and implement into a website please. I just cant seem to find one that actually works anywhere. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/205991-file-upload-a-working-example/ Share on other sites More sharing options...
dabaR Posted June 27, 2010 Share Posted June 27, 2010 http://www.tizag.com/phpT/fileupload.php Quote Link to comment https://forums.phpfreaks.com/topic/205991-file-upload-a-working-example/#findComment-1077865 Share on other sites More sharing options...
therelelogo Posted June 27, 2010 Author Share Posted June 27, 2010 thanks for that when i try to use that example though i get this error: Parse error: syntax error, unexpected T_VARIABLE in /home/a9203152/public_html/uploader.php on line 3 which refers to this line $target_path = "uploads/"; which is part of this code <? php // Where the file is going to be placed $target_path = "uploads/"; /* Add the original filename to our target path. Result is "uploads/filename.extension" */ $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } ?> i have used tizag before and found them very useful and easy to understand, maybe i'm missing something very n00bish here? Quote Link to comment https://forums.phpfreaks.com/topic/205991-file-upload-a-working-example/#findComment-1077866 Share on other sites More sharing options...
dabaR Posted June 27, 2010 Share Posted June 27, 2010 There should not be a space between <? and php Quote Link to comment https://forums.phpfreaks.com/topic/205991-file-upload-a-working-example/#findComment-1077867 Share on other sites More sharing options...
therelelogo Posted June 27, 2010 Author Share Posted June 27, 2010 whoops...i knew that okay, but now i get this error: Warning: move_uploaded_file(uploads/neuve.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/a9203152/public_html/uploader.php on line 9 which is this line if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { and i have created the relevant "uploads"/folder which is accessible to the form. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/205991-file-upload-a-working-example/#findComment-1077869 Share on other sites More sharing options...
dabaR Posted June 27, 2010 Share Posted June 27, 2010 I take it you are working on linux? What is the name of the web server user? You can do ps aux to find out. Quote Link to comment https://forums.phpfreaks.com/topic/205991-file-upload-a-working-example/#findComment-1077870 Share on other sites More sharing options...
therelelogo Posted June 27, 2010 Author Share Posted June 27, 2010 sorry, i'm new to php, linux? im using 000.webhost.com as my server if that helps? i'm not really sure what your asking Quote Link to comment https://forums.phpfreaks.com/topic/205991-file-upload-a-working-example/#findComment-1077871 Share on other sites More sharing options...
therelelogo Posted June 27, 2010 Author Share Posted June 27, 2010 sorry for the double post yet again, but i got this solved and wanted to post to let other people with similar problems know how i did it basically with my server (000.webhost.com) theres a section that allows file permissions to be changed and to chmod it to 777 - easy peasy and also changed the target path to absolute ($target_path = "/home/whatever_goes_here/public_html/uploads/"; and it worked fine, the image uploaded like it was supposed to. however - the fun doesnt stop there. can anybody tell me, say the file i uploaded was "uploads/test_image.jpg" can i write this to a mysql table (the path) and then have a code read it and display the image? i'm assuming it's possible but is it as straight forward as that? thanks Quote Link to comment https://forums.phpfreaks.com/topic/205991-file-upload-a-working-example/#findComment-1077888 Share on other sites More sharing options...
dabaR Posted June 27, 2010 Share Posted June 27, 2010 Yes, it is as straightforward as that. Quote Link to comment https://forums.phpfreaks.com/topic/205991-file-upload-a-working-example/#findComment-1077917 Share on other sites More sharing options...
therelelogo Posted June 27, 2010 Author Share Posted June 27, 2010 okay last Q on this topic i promise. my code is now: <?php // Make a MySQL Connection ************************ $query = "SELECT * FROM adverts WHERE item_id = '$_GET[id]'"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "". $row['image_location'] . ""; echo "<br />"; echo "<img src=\"uploads/new2.jpg\" border=0>"; } ?> how do i change the line echo "<img src=\"uploads/new2.jpg\" border=0>"; so that the image source is reading the value from 'image_location' column...and hopfully disply the image that matches the image stored in that location? p.s - anyway to amend the echo "<img src=\"uploads/new2.jpg\" border=0>"; line so that i can scale the image? i tried just defining the height and width like this: <img src="<?php echo "uploads/new2.jpg" ?>" height="100" width="100"/> but there seems to be a problem with the wording? the first example wont accept it but the second one will thanks again Quote Link to comment https://forums.phpfreaks.com/topic/205991-file-upload-a-working-example/#findComment-1077950 Share on other sites More sharing options...
therelelogo Posted June 27, 2010 Author Share Posted June 27, 2010 again with the double post, however i believe that users should always post solutions so that others can benefit from them. if anyone was using my code, the solution to my last problem can be solved by changing the code to: $query = "SELECT * FROM adverts WHERE item_id = '$_GET[id]'"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "". $row['image_location'] . ""; echo "<br />"; echo "<img src=".$row['image_location']." height=100 width=100 border=0>"; } ?> thanks for helping earlier though Quote Link to comment https://forums.phpfreaks.com/topic/205991-file-upload-a-working-example/#findComment-1077975 Share on other sites More sharing options...
dabaR Posted June 27, 2010 Share Posted June 27, 2010 Hi again, echo "". $row['image_location'] . ""; seems unnecessarily chatty. Seems like echo $row['image_location']; would do the same. I would rewrite echo "<img src=".$row['image_location']." height=100 width=100 border=0>"; as printf('<img src="%s" height="100" width="100" style="border: 0" />', $row['image_location']); Because I think that is more solid HTML. Quote Link to comment https://forums.phpfreaks.com/topic/205991-file-upload-a-working-example/#findComment-1077982 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.