|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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? |
|
#3
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Uploading files/storing file names in Mysql |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|