View Full Version : uploading images with PHP
karinne
01-28-2003, 12:56 PM
I have a news script that allows people to add news to a site. The information is then stored in a PostgreSQL database and spitted out to the respective page.
I want the users to be able to upload images but I have no clue where to look and how to start. Would it upload to a specified folder and then the path stored in the db?
If someone could direct me to a tutorial or give me info on this that'd be great.
Thanks
scoutt
01-28-2003, 06:00 PM
well that is the idea. store the image on the drive and the link to the image in the db.
the manual has a great starting script for you to learn.
karinne
01-29-2003, 09:48 AM
manual!??! what manual!? :confused:
scoutt
01-29-2003, 10:24 AM
www.php.net
karinne
01-29-2003, 10:56 AM
oh.... that manual! Thanks scoutt!:P
scoutt
01-29-2003, 11:36 AM
hehe sorry I should have said that in the first palce. :D
karinne
01-29-2003, 12:52 PM
Am I missing something here!?
<?
if ($HTTP_POST_VARS{'add_image'}) {
$base_path = "/home/www/nouvelles/images/";
copy( $image, $base_path);
$image_path = "/nouvelles/images/" . $image;
$result = pg_exec ("insert into nouvelles (image) values ('$image_path')");
exit;
} else { ?>
<form method="post" enctype="multipart/form-data">
Upload - <input type="file" name="image">
<input type="submit" name="add_image" value="add image">
</form>
<? } ?>
I get this message
Warning: Unable to create '/nouvelles/images//tmp/phpZzi478': No such file or directory
scoutt
01-29-2003, 01:00 PM
yes, you have 2 // in the path
also don't use {} for the POST variables, you need [ ] instead
<?
if ($HTTP_POST_VARS['add_image']) {
$base_path = "/home/www/nouvelles/images";
copy( $image, $base_path);
$image_path = "/nouvelles/images/" . $image;
$result = pg_exec ("insert into nouvelles (image) values ('$image_path')");
exit;
} else { ?>
<form method="post" enctype="multipart/form-data">
Upload - <input type="file" name="image">
<input type="submit" name="add_image" value="add image">
</form>
<? } ?>
karinne
01-29-2003, 01:57 PM
In case someone else needed help to upload 1 image and store the path in a database... here's the code!
<?
if ($HTTP_POST_VARS['add_image']) {
$base_path = "/home/www/mysite/nouvelles/images/".$image_name;
copy($_FILES['image']['tmp_name'], $base_path);
chmod ($base_path, 0644);
$image_path = "/nouvelles/images/" . $image_name;
$result = pg_exec ("insert into table (image) values ('$image_path') ");
exit;
} else {?>
<form method="post" enctype="multipart/form-data">
Upload image - <input type="file" name="image">
<input type="submit" name="add_image" value="add this image">
</form>
<?}?>
;)
scoutt
01-29-2003, 02:04 PM
you are using 2 different ways in one ***ntion. you have $image_name which would be correct if register_globals is ON, but then you used $_FILES[] which is used when register_globals is OFF. best guess it wouldn't work if you had register_globals OFF because you won't get the image_name. and the pg_exec is not a php function but maybe a postreg one.
this is what it should be
<?
if ($HTTP_POST_VARS['add_image']) {
$base_path = "/home/www/mysite/nouvelles/images/".$_FILES['image']['name'];
copy($_FILES['image']['tmp_name'], $base_path);
chmod ($base_path, 0644);
$image_path = "/nouvelles/images/" . $_FILES['image']['name'];
$result = pg_exec ("insert into table (image) values ('$image_path') ");
exit;
} else {?>
<form method="post" enctype="multipart/form-data">
Upload image - <input type="file" name="image">
<input type="submit" name="add_image" value="add this image">
</form>
<?}?>
karinne
01-29-2003, 04:58 PM
hmmm.... interesting... I did not know that! Thanks.... works both ways but I changed it to what you said... good point!
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.