Search
Left Quote    I find that the harder I work, the more luck I seem to have.
- Thomas Jefferson    
Right Quote
 
[login] | [Register]
Forum Index » PHP and mySQL » Viewing Topic and Replies
Post Reply


        Viewing Topic: Uploading Image Files with PHP
  This user is offline  Julian
  Subject: "Uploading Image Files with PHP" Posted: @ 6:04 pm on Jul 09 2007   
Member #: 369
Rank: User - (3)
Since: 07/09/07
Posts: 3

Hi guys

I found the this script extremely useful on my site. Thank you very much.

I'm new on this and maybe my question is silly but here I go:

I have to upload more than 1 image to my web, I did that copy/paste the code and change [userfile], I know this is not the right way to do it. So if is there a way I can upload multiple images using the same code (converting to function or class) and the store the file names on MySQL database will be great? What I'm doing now is

id | pic1 | pic2 |
1 | userfile | userfile2 | and so on...

I need to store only the file name on the database.

Thanks again guys.


    Viewed: 15,860 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Uploading Image Files with PHP" Posted: @ 8:53 pm on Jul 09 2007    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Hmm, what if you created multiple fields in the form to upload a photo by, name them different (userfile1, userfile2, and so on). Verify each through the process outlined in the article. As far as storing them in the database, it depends on how your DB is setup. Store each as a new row in an "images& quot; table, each referring to the member that submitted them?




-bs0d | AllSyntax.com

    Viewed: 15,830 Times | Reply to This | To top
  This user is offline  gravy
  Subject: "re: Uploading Image Files with PHP" Posted: @ 8:55 pm on Jul 09 2007    

Member #: 294
Rank: User - (63)
Since: 03/07/07
Posts: 66
From: MN

  Julian said...

< i> Hi guys

I found the this script extremely useful on my site. Thank you very much.

I'm new on this and maybe my question is silly but here I go:

I have to upload more than 1 image to my web, I did that copy/paste the code and change [userfile], I know this is not the right way to do it. So if is there a way I can upload multiple images using the same code (converting to function or class) and the store the file names on MySQL database will be great? What I'm doing now is

id | pic1 | pic2 |
1 | userfile | userfile2 | and so on...

I need to store only the file name on the database.

Thanks again guys.


the function would be something like this:

Code:

if(!isset($_SES SION['username ']) && !isset($_SESSIO N['password'] )) {
$_SESSION['log ged_in'] = 0;
$user = "Guest&q uot;;
}else if ($_SESSION['lo gged_in'] == 1 ) {
$user = $_SESSION['use rname'];
$image = $_POST["i mage"]; add_photo($user , $filename);
}
// Track file for user
function add_photo($user , $filename){
// Insert a row of information into the table
mysql_query(&q uot;INSERT INTO files (username, image) VALUES ('$user', '$filename') "
or die(mysql_error ());





Photo Hosting Scripts? It's what I do....

    Viewed: 15,828 Times | Reply to This | To top
  This user is offline  Julian
  Subject: "re: Uploading Image Files with PHP" Posted: @ 9:07 pm on Jul 09 2007    
Member #: 369
Rank: User - (3)
Since: 07/09/07
Posts: 3

  bs0d said...

< i> Hmm, what if you created multiple fields in the form to upload a photo by, name them different (userfile1, userfile2, and so on). Verify each through the process outlined in the article. As far as storing them in the database, it depends on how your DB is setup. Store each as a new row in an "images& quot; table, each referring to the member that submitted them?

Thanks for the help! Very much appreciate.

Here's my situation: I did create multiple fields, and named as you recommended but the form only process the "userfile " not the userfile2, userfile3, etc. I'll will include the code maybe you can understand better what I'm doing:


Code:
//------------ ------------Ima genes---------- --------------< br /> if(isset($_POST ['submit'])) {
if($_FILES['us erfile']['siz e'] > 1) {

$info = getimagesize($_ FILES['userfil e']['tmp_name ']);

//check the extension.
$array = explode(" .", $_FILES['userf ile']['name' ]);
$nr = count($array);
$ext = $array[$nr-1];< br /> if(($ext !="jpg&q uot;) && ($ext !="jpeg& quot;) && ($ext !="png&q uot;))
die("< BR><BR> ;Error: la extension del archivo no es reconocida. Asegurese que este utilizando la imagen correcta (.JPG or .PNG)");< br />
//CHECK TYPE: (what the browser sent)
if(($_FILES['u serfile']['ty pe'] != "image/jp eg") && ($_FILES['user file']['type '] != "image/pj peg") && ($_FILES['user file']['type '] != "image/pn g")) {
die("< ;BR><BR&g t;Error: El tipo de imagen no es reconocido. Solamente imagenes .JPG o .PNG son permitidas.&qu ot;);
}

//DOUBLE CHECK TYPE: if image MIME type from GD getimagesize() -In case it was a FAKE!
if(($info['mim e'] != "image/jp eg") && ($info['mime' ] != "image/pj peg") && ($info['mime' ] != "image/pn g")) {
die("< ;BR><BR&g t;Error: El tipo de imagen no es reconocido. Solamente imagenes .JPG o .PNG son permitidas.&qu ot;);
}

//rename file, move it to location.
if(is_uploaded_ file($_FILES[' userfile']['t mp_name'])) {

$file_name = $_FILES['userf ile']['name' ];

//create a random file name
$rand_name = md5(time());
$rand_name .= rand(0,99999999 9);
}

//Subir el Archivo
move_uploaded_ file($_FILES[' userfile']['t mp_name'] , $_SERVER['DOCU MENT_ROOT'].& quot;/admin/fot os/".$ran d_name . '.' . $ext);

$filename=$ran d_name . '.' . $ext;

}}

//------------- -----------Imag enes----------- -------------
if ((isset($_POST[ "MM_inser t"])) && ($_POST[" MM_insert" ;] == "form1&q uot;)) {
$insertSQL = sprintf(" INSERT INTO local (local, status, id_zona, direccion, horario, descripcion, precios, calificacion, id_food, telefono, fax, email, contacto, otros, pic1, pic2) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueStri ng($_POST['loc al'], "text&qu ot;),
GetSQLValueStri ng(isset($_POST ['status']) ? "true&qu ot; : "", "defined ","1 "," 0"),
GetSQLValueStri ng($_POST['id_ zona'], "int&quo t;),
GetSQLValueStri ng($_POST['dir eccion'], "text&qu ot;),
GetSQLValueStri ng($_POST['hor ario'], "text&qu ot;),
GetSQLValueStri ng($_POST['des cripcion'], "text&qu ot;),
GetSQLValueStri ng($_POST['pre cios'], "text&qu ot;),
GetSQLValueStri ng($_POST['cal ificacion'], "int&quo t;),
GetSQLValueStri ng($foods, "text&qu ot;), //Arreglo de foods
GetSQLValueStri ng($_POST['tel efono'], "text&qu ot;),
GetSQLValueStri ng($_POST['fax '], "text&qu ot;),
GetSQLValueStri ng($_POST['ema il'], "text&qu ot;),
GetSQLValueStri ng($_POST['con tacto'], "text&qu ot;),
GetSQLValueStri ng($_POST['otr os'], "text&qu ot;),
GetSQLValueStri ng($filename, "text&qu ot;));




    Viewed: 15,825 Times | Reply to This | To top
  This user is offline  gravy
  Subject: "re: Uploading Image Files with PHP" Posted: @ 9:31 pm on Jul 09 2007    

Member #: 294
Rank: User - (63)
Since: 03/07/07
Posts: 66
From: MN

  Julian said...

< i>   bs0d said... [= Hmm, what if you created multiple fields in the form to upload a photo by, name them different (userfile1, userfile2, and so on). Verify each through the process outlined in the article. As far as storing them in the database, it depends on how your DB is setup. Store each as a new row in an "images& quot; table, each referring to the member that submitted them?

Thanks for the help! Very much appreciate.

Here's my situation: I did create multiple fields, and named as you recommended but the form only process the "userfile " not the userfile2, userfile3, etc. I'll will include the code maybe you can understand better what I'm doing:

<BR><f ont size=1><u >Code:</u ><BR>& lt;div name="div _box" class="di vCode"> ;//------------ ------------Ima genes---------- --------------
if(isset($_POST ['submit'])) {
if($_FILES['us erfile']['siz e'] > 1) {

$info = getimagesize($_ FILES['userfil e']['tmp_name ']);

//check the extension.
$array = explode(" .", $_FILES['userf ile']['name' ]);
$nr = count($array);
$ext = $array[$nr-1];
if(($ext !="jpg&q uot && ($ext !="jpeg& quot && ($ext !="png&q uot)
die("< BR><BR> ;Error: la extension del archivo no es reconocida. Asegurese que este utilizando la imagen correcta (.JPG or .PNG)";

//CHECK TYPE: (what the browser sent)
if(($_FILES['u serfile']['ty pe'] != "image/jp eg" && ($_FILES['user file']['type '] != "image/pj peg" && ($_FILES['user file']['type '] != "image/pn g") {
die("< ;BR><BR&g t;Error: El tipo de imagen no es reconocido. Solamente imagenes .JPG o .PNG son permitidas.&qu ot;
}

//DOUBLE CHECK TYPE: if image MIME type from GD getimagesize() -In case it was a FAKE!
if(($info['mim e'] != "image/jp eg" && ($info['mime' ] != "image/pj peg" && ($info['mime' ] != "image/pn g") {
die("< ;BR><BR&g t;Error: El tipo de imagen no es reconocido. Solamente imagenes .JPG o .PNG son permitidas.&qu ot;
}

//rename file, move it to location.
if(is_uploaded_ file($_FILES[' userfile']['t mp_name'])) {

$file_name = $_FILES['userf ile']['name' ];

//create a random file name
$rand_name = md5(time());
$rand_name .= rand(0,99999999 9);
}

//Subir el Archivo
move_uploaded_ file($_FILES[' userfile']['t mp_name'] , $_SERVER['DOCU MENT_ROOT'].& quot;/admin/fot os/".$ran d_name . '.' . $ext);

$filename=$ran d_name . '.' . $ext;

}}

//------------- -----------Imag enes----------- -------------

if ((isset($_POST[ "MM_inser t"])) && ($_POST[" MM_insert" ;] == "form1&q uot) {
$insertSQL = sprintf(" INSERT INTO local (local, status, id_zona, direccion, horario, descripcion, precios, calificacion, id_food, telefono, fax, email, contacto, otros, pic1, pic2) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueStri ng($_POST['loc al'], "text&qu ot,
GetSQLValueStri ng(isset($_POST ['status']) ? "true&qu ot; : "", "defined ","1 "," 0",
GetSQLValueStri ng($_POST['id_ zona'], "int&quo t,
GetSQLValueStri ng($_POST['dir eccion'], "text&qu ot,
GetSQLValueStri ng($_POST['hor ario'], "text&qu ot,
GetSQLValueStri ng($_POST['des cripcion'], "text&qu ot,
GetSQLValueStri ng($_POST['pre cios'], "text&qu ot,
GetSQLValueStri ng($_POST['cal ificacion'], "int&quo t,
GetSQLValueStri ng($foods, "text&qu ot, //Arreglo de foods
GetSQLValueStri ng($_POST['tel efono'], "text&qu ot,
GetSQLValueStri ng($_POST['fax '], "text&qu ot,
GetSQLValueStri ng($_POST['ema il'], "text&qu ot,
GetSQLValueStri ng($_POST['con tacto'], "text&qu ot,
GetSQLValueStri ng($_POST['otr os'], "text&qu ot,
GetSQLValueStri ng($filename, "text&qu ot);
=]
You need to loop it so that it processes each file...




Photo Hosting Scripts? It's what I do....

    Viewed: 15,821 Times | Reply to This | To top
  This user is offline  Julian
  Subject: "re: Uploading Image Files with PHP" Posted: @ 9:43 pm on Jul 09 2007    
Member #: 369
Rank: User - (3)
Since: 07/09/07
Posts: 3

Exactly, that's what I want to do... but how? because I have upload each image and store each name on the database...

Thanks




    Viewed: 15,818 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Uploading Image Files with PHP" Posted: @ 11:24 pm on Jul 09 2007    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

FYI: for posting, if you click "Disable Smilies" check box at the bottom, it will prevent smilies in code.




-bs0d | AllSyntax.com

    Viewed: 15,810 Times | Reply to This | To top
  This user is offline  abhi
  Subject: "Similar : Uploading Files with PHP" Posted: @ 2:10 pm on Nov 19 2007    
Member #: 379
Rank: User - (19)
Since: 07/30/07
Posts: 19
From: Michigan

I have a similar question.Can anyone please tell me how to move all the files to a common directory and then give a command in PHP where it will upload all the files one by one from the directory and I dont have to enter the file names one by one.I run the following command where the last part xyz.txt is the filename .I change it everytime there is a new file.Herez the command:
php actimp.php emu_dw/*******@ IRIM1 szt_w_act.ctl xyz.txt.



Edited at 02:15:18 pm on 11/19/07


abhishekitty@ya
hoo.com |     Viewed: 14,618 Times | Reply to This | To top
  This user is offline  ryan_pee
  Subject: " Uploading word Files with PHP" Posted: @ 8:08 pm on Nov 21 2007    
Member #: 483
Rank: User - (1)
Since: 11/21/07
Posts: 1

Hello, i'm new to this forum and was wondering if anyone could please tell me how to upload a doc file.

Thanks alot




    Viewed: 14,589 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Uploading Image Files with PHP" Posted: @ 9:39 pm on Nov 21 2007    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Sure, read our article on Uploading Image Files with PHP. Instead of uploading .JPG and .PNG, change the coding to allow only .DOC files. The script can upload anything, you just regulate what to accept and what not to accept. So its univeral that way. If you have any questions, feel free to ask. Thanks




-bs0d | AllSyntax.com

    Viewed: 14,585 Times | Reply to This | To top
Viewing Page: 1 of 1


1 |

You must be logged in to post on the forums. Login or Register








"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank