Page 1 of 2
Upload image and store path
Posted: Thu Jul 09, 2009 10:04 am
by StefanAlexandru
Hy everyone,
I have a problem with a php script that uploads 3 images to a upload directory and stores the image path to mysql database, i found the upload script on a forum, but it seems is not working. here is the script:
Html Form:
Code: Select all
<form enctype="multipart/form-data" action="upload.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="99999999" />
<!-- Name of input element determines name in $_FILES array -->
Picture1 <input name="picture1" type="file" />
Picture 2 <input name="picture2" type="file" />
Picture 3 <input name="picture3" type="file" />
<input type="submit" value="Upload" />
</form>
and this is upload.php
Code: Select all
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("eurolevel")
?>
<?
$uploaddir = 'EuroLevel/apartamente/';
$uploadfile = $uploaddir . basename($_FILES['poza1']['poza2'] ['poza3']);
if (move_uploaded_file($_FILES['picture1'] ['picture2'] ['picture3'] ['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "There was an error\n";
}
$res = mysql_query("insert into apartamente values(picture1, picture2, picture3);");
echo 'Here is some more debugging info:';
print_r($_FILES);
?>
Thanks in advance, and sorry for my bad english
Re: Upload image and store path
Posted: Thu Jul 09, 2009 10:09 am
by VladSun
1. Use absolute paths
2. You can't use move_uploaded_file() to move more than one file at a time.
3. This: $_FILES['picture1'] ['picture2'] ['picture3'] is absolutely wrong. You need a little read about PHP syntax

Re: Upload image and store path
Posted: Thu Jul 09, 2009 10:19 am
by StefanAlexandru
thanks, but what should i use instead of move_uploaded_file() ?
Can you give a site or a tutorial where can i learn ?
Thanks a lot!
Re: Upload image and store path
Posted: Thu Jul 09, 2009 10:21 am
by VladSun
Re: Upload image and store path
Posted: Thu Jul 09, 2009 2:18 pm
by StefanAlexandru
okey i finally made my upload script it looks like :
Code: Select all
<?php
$path1= "apartamente/".$HTTP_POST_FILES['ufile']['name'][0];
$path2= "apartamente/".$HTTP_POST_FILES['ufile']['name'][1];
$path3= "apartamente/".$HTTP_POST_FILES['ufile']['name'][2];
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);
$filesize1=$HTTP_POST_FILES['ufile']['size'][0];
$filesize2=$HTTP_POST_FILES['ufile']['size'][1];
$filesize3=$HTTP_POST_FILES['ufile']['size'][2];
if($filesize1 && $filesize2 && $filesize3 != 0)
{
echo "We have recieved your files";
}
else {
echo "ERROR.....";
}
?>
but now i need help how to store the path in mysql, can anybody explain or show me the code for this ?
Thanks a lot in advance
Re: Upload image and store path
Posted: Thu Jul 09, 2009 2:33 pm
by VladSun
http://bg.php.net/manual/en/reserved.va ... .files.php
4.1.0 Introduced $_FILES that deprecated $HTTP_POST_FILES
So ... don't use $HTTP_POST_FILES, use $_FILES instead
Could you, please, post the whole code snippet for uploading files?
Re: Upload image and store path
Posted: Thu Jul 09, 2009 2:47 pm
by StefanAlexandru
this is the whole code.. i have a form :
Code: Select all
<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="adauga.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><strong>multiple Files Upload </strong></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
and that is the upload php.. i will change that value
LATER EDIT :
this is the original code, but i have deleted the parts that i don't need
Code: Select all
<?php
//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
$path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];
$path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2];
//copy file to where you want to store file
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);
//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][0]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>";
echo "<img src=\"$path1\" width=\"150\" height=\"150\">";
echo "<P>";
echo "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>";
echo "<img src=\"$path2\" width=\"150\" height=\"150\">";
echo "<P>";
echo "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>";
echo "<img src=\"$path3\" width=\"150\" height=\"150\">";
///////////////////////////////////////////////////////
// Use this code to display the error or success.
$filesize1=$HTTP_POST_FILES['ufile']['size'][0];
$filesize2=$HTTP_POST_FILES['ufile']['size'][1];
$filesize3=$HTTP_POST_FILES['ufile']['size'][2];
if($filesize1 && $filesize2 && $filesize3 != 0)
{
echo "We have recieved your files";
}
else {
echo "ERROR.....";
}
//////////////////////////////////////////////
// What files that have a problem? (if found)
if($filesize1==0) {
echo "There're something error in your first file";
echo "<BR />";
}
if($filesize2==0) {
echo "There're something error in your second file";
echo "<BR />";
}
if($filesize3==0) {
echo "There're something error in your third file";
echo "<BR />";
}
?>
Re: Upload image and store path
Posted: Thu Jul 09, 2009 3:07 pm
by VladSun
OK
So ... Let's clarify the upload part of the script:
1.
VladSun wrote:Use absolute paths
2.
VladSun wrote:don't use $HTTP_POST_FILES, use $_FILES instead
3. use move_uploaded_file() instead of copy() function
4. did you read the links I googled for you? There are some very useful and almost (because it's walways "almost") secure file upload scripts - I would recommend
http://www.scanit.be/uploads/php-file-upload.pdf
So, fix these issues and let us continue to the DB part.
Re: Upload image and store path
Posted: Thu Jul 09, 2009 3:28 pm
by StefanAlexandru
what would be my absolute path if I use xamp... C:\xampp\htdocs\eurolevel\apartamente ' where apartamente is my upload directory and eurolevel the directory with upload script
okay i have changed $HTTP_POST_FILES with $_FILES and also i have changed copy with move_uploaded_file()
and now the code look like
Code: Select all
<?php
$path1= "apartamente/".$_FILES['ufile']['name'][0];
$path2= "apartamente/".$_FILES['ufile']['name'][1];
$path3= "apartamente/".$_FILES['ufile']['name'][2];
move_uploaded_file($_FILES['ufile']['tmp_name'][0], $path1);
move_uploaded_file($_FILES['ufile']['tmp_name'][1], $path2);
move_uploaded_file($_FILES['ufile']['tmp_name'][2], $path3);
$filesize1=$_FILES['ufile']['size'][0];
$filesize2=$_FILES['ufile']['size'][1];
$filesize3=$_FILES['ufile']['size'][2];
if($filesize1 && $filesize2 && $filesize3 != 0)
{
echo "We have recieved your files";
}
else {
echo "ERROR.....";
}
?>
I have read that PDF and i will make the changes to my script for prevent hacking.. thanks a lot for helping me! but now please let's go to database part, because i am very curios about it
Re: Upload image and store path
Posted: Thu Jul 09, 2009 3:31 pm
by VladSun
StefanAlexandru wrote:let's go to database part, because i am very curios about it[/b]
What have you tried so far?
Re: Upload image and store path
Posted: Thu Jul 09, 2009 3:37 pm
by StefanAlexandru
i didn't try anything concrete, the only thing i know is that i should have a code like this
Code: Select all
mysql_connect("localhost", "root", "password");
mysql_select_db("eurolevel");
mysql_query("INSERT INTO apartamente VALUES('picture1', 'picture2', 'picture3'");
i know that is incomplete, that is why i need help

..
Re: Upload image and store path
Posted: Thu Jul 09, 2009 3:50 pm
by VladSun
Show use the table apartamente structure - you need something like:
id | fileName
where id is INT auto_increment and fileName is varchar(100)
Re: Upload image and store path
Posted: Thu Jul 09, 2009 3:57 pm
by StefanAlexandru
where poza1 poza2 and poza3 are fields where i want to store the paths of the pictures
and id is already primary key
Later Edit:
i will modify to varchar 100 right now

Re: Upload image and store path
Posted: Thu Jul 09, 2009 4:04 pm
by VladSun
StefanAlexandru wrote:i will modify to varchar 100 right now

It's ok right now - you don't need to modify it
Try :
Code: Select all
mysql_query("INSERT INTO apartamente (poza1, poza2, poza3) VALUES('$path1', '$path2', 'path3'");
You need to sanitize $path* variables.
Also, you need to insert any other additional information required by the DB table design.
Re: Upload image and store path
Posted: Thu Jul 09, 2009 4:15 pm
by StefanAlexandru
can you be more explicit about sanitize ?
i have tried inserting the line but is not working, to work it have to insert all other information?