Jump to content

File upload - a working example?


therelelogo

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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  :shrug:

 

thanks again

Link to comment
Share on other sites

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  :D

Link to comment
Share on other sites

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.

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.