Jump to content

Recommended Posts

[!--quoteo(post=367683:date=Apr 23 2006, 03:18 PM:name=Amanda Griffiths)--][div class=\'quotetop\']QUOTE(Amanda Griffiths @ Apr 23 2006, 03:18 PM) [snapback]367683[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi all,

Please could somebody help me to store an image link in mysql database instead of storing the actual image.

EG. users uploads file which goes into images folder on webserver, and at same time, a link to it gets saved in the db for future reference.

Thanks
[/quote]

Hi
Which part are you stuck on? are you able to upload the file to the correct place on the web server?

if so, then it's simply a case of saving the filename you used within 'move_uploaded_file' in your database, along with any other information from the $_FILES array you want (such as size, type, original filename, etc).

if you can give a better idea of which part you're having trouble with, it maybe easier to help you out.

cheers
Mark
[!--quoteo(post=367686:date=Apr 23 2006, 03:28 PM:name=Amanda Griffiths)--][div class=\'quotetop\']QUOTE(Amanda Griffiths @ Apr 23 2006, 03:28 PM) [snapback]367686[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I can store the uploaded file into the images directory on the webserver, but then i don't know how to store the link to the image into the database

Many Thanks
[/quote]

hi
if you've used move_uploaded_file to do the job (which it sounds like you have), then youre half way there.
the second parameter you use (ie, the destination path/filename) is all you need to store and retrieve when you need it.
generally when i'm storing images on my server (from uploads) i keep the upload path the same (and set by a variable in an include file, such as $upload_path), so then all i need to store in my database is the destination filename and not any path information. when i wanna retrieve them, i can either reference them as a url (ie, [a href=\"http://www.mysite.com/uploads/\" target=\"_blank\"]http://www.mysite.com/uploads/[/a][b]myimage.jpg[/b]) or as a full path (/myserverpath/public_html/uploads/[b]myimage.jpg[/b]).

is it the actual mysql stuff youre having trouble with?

cheers
Mark
Thanks for the help mark

I am using dreamweaver, and when i try to upload the picture, i get the error...here is my code

<form action="/admin/upload_script.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
Select Image
<label>
<input type="file" name="file" />
<br />
<input type="submit" name="Submit" value="Submit" />
</label>
</form>

and the handling script...

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = 'c:/domains/xxxxxxxxx/wwwroot/images/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>

Now i'm stuck before i even get to the part of adding the link into the database :(

Thanks again
hi,
not sure what error youre getting, but i'm guessing it's gonna be a permissions thing. have you made sure that the c:/...blahblah.../images directory is 'writable' by the script? by default, it wont be, so you'll need to change this so that you can write to the images directory.

try that first, see how it goes, then onto round two...

cheers
Mark
[!--quoteo(post=367693:date=Apr 23 2006, 03:59 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Apr 23 2006, 03:59 PM) [snapback]367693[/snapback][/div][div class=\'quotemain\'][!--quotec--]
hi,
not sure what error youre getting, but i'm guessing it's gonna be a permissions thing. have you made sure that the c:/...blahblah.../images directory is 'writable' by the script? by default, it wont be, so you'll need to change this so that you can write to the images directory.

try that first, see how it goes, then onto round two...

cheers
Mark
[/quote]


Thanks again Mark,

I know have images uploading into the correct directory on the web server, and i have got the path saved in the db.

Any ideas what is wrong here though...it doesn't want to upload the other information which goes with the images..

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fileArtname = $_FILES['userfile']['artist_name'];
$fileAlbtitle = $_FILES['userfile']['album_title'];
$fileAlbinfo = $_FILES['userfile']['album_info'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}


if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO upload2 (name, size, type, path, artist_name, album_title, album_info) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$fileArtname', '$fileAlbtitle', '$fileAlbinfo')";

It inserts the image, the path and the filename, but none of the rest.

Thanks
i don't get this bit.
[code]
$fileArtname = $_FILES['userfile']['artist_name'];
$fileAlbtitle = $_FILES['userfile']['album_title'];
$fileAlbinfo = $_FILES['userfile']['album_info'];
[/code]
artist_name, album_title, etc, arent proper values for the $_FILES array. maybe you should be using $_POST['album_title'], $_POST['album_info'] , etc, instead...

cheers
Mark
Thanks Mark,

The post statements did the trick.

I just need to figure out the best way of calling the info back and displaying it on screen.

I don't suppose you have a line or 2 of code to set me in the right direction, as i'm not sure about how to disp[lay the uploaded image via the link which is stored in the db.

Many Thanks again
[!--quoteo(post=367717:date=Apr 23 2006, 05:51 PM:name=Amanda Griffiths)--][div class=\'quotetop\']QUOTE(Amanda Griffiths @ Apr 23 2006, 05:51 PM) [snapback]367717[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Thanks Mark,

The post statements did the trick.

I just need to figure out the best way of calling the info back and displaying it on screen.

I don't suppose you have a line or 2 of code to set me in the right direction, as i'm not sure about how to disp[lay the uploaded image via the link which is stored in the db.

Many Thanks again
[/quote]

Hi
Basically:
(ignore the syntax i've used - its purely to give an idea...)

[code]
the php bit:

<?php
$query = "select * from the_database where id = '$the_image_id_i_want'";
$result = mysql_query($query);
$picture = mysql_fetch_assoc($result);
?>

the html bit:
<img src="<?php echo $picture['path']; ?>" />

[/code]

the only thing to watch in the above example is how the 'path' was stored in the DB in the first place. i.e, is it a URL (http://blahblah.com/images/blah.jpg) or a full path (i.e, /myserver/wwwroot/blah.jpg). if it's the former, your fine.
It's the full path...

C:/domains/xxxxxxx/wwwroot/images/givea....jpg

So far i have this to output the data from every record...


$sql = "SELECT * FROM upload2";
$data = mysql_query($sql);



while($my_data = mysql_fetch_array($data))
{

printf("%s,<br> %s,<br> %s,<br> %s, <br />",
$my_data['artist_name'], $my_data['album_title'], $my_data['album_info'], $my_data['path']);
}


This is fine, although a bit nasty looking, but i will sort that after i have everything listing ok....probably use tables to lay it out.

I can't seem to find a way of turning the image path, into the actual image though.

Thanks

I have managed to change the stored url to ../images/erg4tr.jpg now, but if i change it to [a href=\"http://www.sgrt.co.uk/images/\" target=\"_blank\"]http://www.sgrt.co.uk/images/[/a], then it doesn't work
well the way you've stored your paths is relative to the server root, so is not a URL. the <img> tag needs a URL or a path relative to the site/file. eg: <img src="http://site.com/images/image.jpg" /> or <img src="/images/image.jpg" /> or <img src="../images/image.jpg" />

the 'basename' function you used should do the trick. just do:

[code]
<?php
// following line could also be http://mysite.com/images/
$upload_path = "/images/";

... get the database stuff here ...

$filename = $upload_path.basename($my_data['path']);
?>


<img src="<?php echo $filename; ?>" />


[/code]

that should give you a better idea

[b]edit[/b]: also, i wouldnt recommend you store anything other than the filename in the DB anyway. if you ever change your server or directory names, all the DB filenames will be broken links. if you store the path in variables (for example, in a config.php file which you include into your pages as required), then all you need to store in the DB is just the filename (eg, myimage.jpg and not c:/blahblah/blah/wwwroot/images/myimage.jpg)

[b]edit[/b]: also, i wouldnt recommend you store anything other than the filename in the DB anyway. if you ever change your server or directory names, all the DB filenames will be broken links. if you store the path in variables (for example, in a config.php file which you include into your pages as required), then all you need to store in the DB is just the filename (eg, myimage.jpg and not c:/blahblah/blah/wwwroot/images/myimage.jpg)
[/quote]


Ok, guess i will start again then as i cant get it working properly anyway.

Just get the image holder, and when i right click it just tells me that the image location should be admin, when it shouldn't.

I'll look aroubnd and try and find out what you are going on about with the config.php as i dont understand, and i should imagine your patience is wearing a bit thin with me know :)

Thanks for all the help
[!--quoteo(post=367728:date=Apr 23 2006, 06:42 PM:name=Amanda Griffiths)--][div class=\'quotetop\']QUOTE(Amanda Griffiths @ Apr 23 2006, 06:42 PM) [snapback]367728[/snapback][/div][div class=\'quotemain\'][!--quotec--]

I'll look aroubnd and try and find out what you are going on about with the config.php as i dont understand, and i should imagine your patience is wearing a bit thin with me know :)

[/quote]

sorry i may have not been clear on this. its just something i do personally when dealing with things like this. basically i have a config.php which i store stuff that's used sitewide. bit like having a CSS stylesheet that stores all your styles - well config.php stores all my settings, including paths. for example:

config.php
[code]
$upload_path = "c:/domains/xxxxxxxxx/wwwroot/images/";
$upload_path_url = "http://www.mysite.com/images/";
[/code]

in your file that deals with the file upload/saving/retrieval, etc, you would just have:
[code]
<?php
include_once('/path/to/config.php');

...rest of code here
?>
[/code]

all it does is just keeps things consistent and in one place, and would remove the need to store full paths in your database. if you moved servers or changed directory names or anything else, all you would need to do is to change the settings in config.php file, rather than having to manually change every record in your database.
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.