PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old June 29th, 2004, 06:28 PM
jdkarns jdkarns is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Dayton, OH
Posts: 12 jdkarns User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Uploading files/storing file names in Mysql

I have a form that collects the variables below like $price, $type, $age, etc. Also, the form uploads 3 files (.jpg). I'm using a multi-file upload snippet I found in the PHP Manual and it works great even renaming the files with regular expressions making the files all lower case including the .jpg extention.

Here is My problem: I cant seem to extract the 3 new file names as variables so I can place them in a Mysql table to call them up in a query?

I want to call the 3 variables:

$pic1 = somefilename?;
$pic2 = somefilename?;
$pic3 = somefilename?;

Then I could insert them into my "Mysql" table named horses.


By the way at the end of my code shown below I have echo'ed the $pic variables to show that the variables were created but no matter what coding I have tried I cant seem to capture the "new file names" as variables.



Thanks for any help in advance


Code:
// Handle price, type, age, sex, description
$price = $_POST['price'];
$type = $_POST['type'];
$age = $_POST['age'];
$sex = $_POST['sex'];
$description = $_POST['description'];


// handle uploaded horse images
$path_to_file = '/home/horse/www/img/';
$files = $_FILES['files'];
if (!ereg("/$", $path_to_file))
        $path_to_file = $path_to_file."/";
foreach ($files['name'] as $key=>$name) {
     
  if ($files['size'][$key]) {
     // clean up file name
     $name = ereg_replace("[^a-z0-9._]", "",
       str_replace(" ", "_",
           str_replace("%20", "_", strtolower($name)
           )
       )
     );
	 $location = $path_to_file.$name;
          while (file_exists($location))
        $location .= ".copy"; 
     copy($files['tmp_name'][$key],$location);
     unlink($files['tmp_name'][$key]);
     echo "\n
Successfully uploaded file: $name.";
  }
}


echo '<br />' . $pic1 . '<br />';
echo '<br />' . $pic2 . '<br />';
echo '<br />' . $pic3 . '<br />';
echo '<br />' . $price . '<br />';
echo '<br />' . $type . '<br />';
echo '<br />' . $age . '<br />';
echo '<br />' . $sex . '<br />';
echo '<br />' . $description . '<br />';

?>

Last edited by jdkarns : June 29th, 2004 at 06:39 PM. Reason: added note about echoing $pic variables

Reply With Quote
  #2  
Old June 30th, 2004, 10:47 AM
jdkarns jdkarns is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Dayton, OH
Posts: 12 jdkarns User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
OMG I figured it out. I got the answer!!

I just added this code for each of the variables that i needed.

$pic1 = $_FILES['userfile']['name'][0];
// clean up file name
$pic1 = ereg_replace("[^a-z0-9._]", "",
str_replace(" ", "_",
str_replace("%20", "_", strtolower($pic1)
)
)
);



Code:
// Handle price, type, age, sex, description
$price = $_POST['price'];
$type = $_POST['type'];
$age = $_POST['age'];
$sex = $_POST['sex'];
$description = $_POST['description'];

// handle uploaded horse images
$path_to_file = '/home/horse/www/img/';
$files = $_FILES['userfile'];

// added variables with the uploaded file names and cleaned
// them up with the same regular expression found in the
// foreach() below
$pic1 = $_FILES['userfile']['name'][0];
     // clean up file name
     $pic1 = ereg_replace("[^a-z0-9._]", "",
       str_replace(" ", "_",
           str_replace("%20", "_", strtolower($pic1)
           )
       )
     );
$pic2 = $_FILES['userfile']['name'][1];
     // clean up file name
     $pic2 = ereg_replace("[^a-z0-9._]", "",
       str_replace(" ", "_",
           str_replace("%20", "_", strtolower($pic2)
           )
       )
     );
$pic3 = $_FILES['userfile']['name'][2];
     // clean up file name
     $pic3 = ereg_replace("[^a-z0-9._]", "",
       str_replace(" ", "_",
           str_replace("%20", "_", strtolower($pic3)
           )
       )
     );


if (!ereg("/$", $path_to_file))
        $path_to_file = $path_to_file."/";
foreach ($files['name'] as $key=>$name) {
     
  if ($files['size'][$key]) {
     // clean up file name
     $name = ereg_replace("[^a-z0-9._]", "",
       str_replace(" ", "_",
           str_replace("%20", "_", strtolower($name)
           )
       )
     );
	 $location = $path_to_file.$name;
          while (file_exists($location))
        $location .= ".copy"; 
     copy($files['tmp_name'][$key],$location);
     unlink($files['tmp_name'][$key]);
     echo "\n Successfully uploaded file: $name.";
  }
}


echo '<br />' . $pic1 . '<br />';
echo '<br />' . $pic2 . '<br />';
echo '<br />' . $pic3 . '<br />';
echo '<br />' . $price . '<br />';
echo '<br />' . $type . '<br />';
echo '<br />' . $age . '<br />';
echo '<br />' . $sex . '<br />';
echo '<br />' . $description . '<br />';
?>


Hope this might help somebody sometime?

Reply With Quote
  #3  
Old June 30th, 2004, 10:52 AM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
jdkarns,

I was going to say that you were missing the code that assigns values to your $pic(x) variables... Glad to see you caught that!

My next suggestion is to omit the regular expression clean-up of the filenames that are being stored in the $_FILES[] superglobal. Try assigning the value directly from the array to your $pic(x) vars, and see what prints out.

Also, what does your upload form code look like? Do you have the enctype="multipart/form-data" within your <form> tag?
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Uploading files/storing file names in Mysql


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway