I have a script that uploads to a specified directory and it’s working nicely. I’m just wondering if it’s possible to upload that same file to multiple directories at the same time?
Here’s a snippet of what i’m trying
if (!file_exists(UPLOAD_DIR.$file)) {
// move the file to the upload folder and rename it
$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$file);
// Can I create another var and use move_uploaded_file 2xs???
$thumbup = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_TMB.$file);
here’s the full script:
if (array_key_exists('upload', $_POST)) {
// define constant for upload folder
define('UPLOAD_DIR', 'file:///Users/my_user/sites/my_dir/images/');
define('UPLOAD_TMB', 'file:///Users/my_user/sites/my_dir/images/thumbs/');
// replace any spaces in original filename with underscores
// at the same time, assign to a simpler variable
$file = str_replace(' ', '_', $_FILES['image']['name']);
// convert the maximum size to KB
$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
// create an array of permitted MIME types
$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
// begin by assuming the file is unacceptable
$sizeOK = false;
$typeOK = false;
// check that the file size is within the permitted size
if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
// check that file is of a permitted MIME type
foreach ($permitted as $type) {
if ($type == $_FILES['image']['type']) {
$typeOK = true;
break;
}
}
if ($sizeOK && $typeOK) {
switch($_FILES['image']['error']) {
case 0:
// make sure file of same name does not already exist
if (!file_exists(UPLOAD_DIR.$file)) {
// move the file to the upload folder and rename it
$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$file);
// Can I create another var and use move_uploaded_file 2xs???
$thumbup = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_TMB.$file);
}
else {
$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.time().$file);
$thumbup = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_TMB.time().$file);
}
if ($success && $thumbup) {
$result = "$file uploaded successfully";
}
else {
$result = "Error uploading $file. Please try again.";
}
break;
case 3:
$result = "Error uploading $file. Please try again.";
default:
$result = "System error uploading $file. Contact webmaster.";
}
}
elseif ($_FILES['image']['error'] == 4) {
$result = 'No file selected';
}
else {
$result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
}
}
any insight would be appreciated by the new learner of PHP. thanks!