|
Posted by petersprc on 11/01/07 10:58
One thing to check is if error_reporting(E_ALL | E_STRICT) gives any
notices. The php script below shows how to handle multiple and single
file uploads.
[upload.php]
<?
// This page will store the uploaded files in a directory
// called "uploads".
error_reporting(E_ALL);
function storeUploads($id, $dir, $debug = false)
{
$ok = true;
$count = 0;
if (is_array($_FILES[$id]['error'])) {
// Multiple files
foreach ($_FILES[$id]['error'] as $key => $err) {
if ($err != UPLOAD_ERR_OK && $err != UPLOAD_ERR_NO_FILE) {
trigger_error("Upload of file $id failed with error " .
"code $err.", E_USER_ERROR);
$ok = false;
} elseif ($err == UPLOAD_ERR_OK) {
$tmp = $_FILES[$id]['tmp_name'][$key];
$name = $_FILES[$id]['name'][$key];
$dest = "$dir/" . basename($name);
if ($debug) {
echo "Storing file: id = $id, tmp = $tmp, name = $name, " .
"dest = $dest<br>";
}
if (!move_uploaded_file($tmp, $dest)) {
trigger_error("Failed to save file to $dest",
E_USER_ERROR);
$ok = false;
} else {
$count++;
}
}
}
} else {
// Single file
$err = $_FILES[$id]['error'];
if ($err != UPLOAD_ERR_OK && $err != UPLOAD_ERR_NO_FILE) {
trigger_error("Upload of file $id failed with error code " .
"$err.", E_USER_ERROR);
$ok = false;
} elseif ($err == UPLOAD_ERR_OK) {
$tmp = $_FILES[$id]['tmp_name'];
$name = $_FILES[$id]['name'];
$dest = "$dir/" . basename($name);
if ($debug) {
echo "Storing file: id = $id, tmp = $tmp, name = $name, " .
"dest = $dest<br>";
}
if (!move_uploaded_file($tmp, $dest)) {
trigger_error("Failed to save file to $dest",
E_USER_ERROR);
$ok = false;
} else {
$count++;
}
}
}
return $ok ? $count : $ok;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$count = storeUploads('file', dirname(__FILE__) . '/uploads',
true);
echo "Stored $count " . ($count == 1 ? 'file' : 'files') .
'.<br>';
}
?>
<h3>Single Upload</h3>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="submit" value=" OK ">
</p>
</form>
<h3>Multiple Uploads</h3>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file[]"><br>
<input type="file" name="file[]"><br>
<input type="file" name="file[]"><br><br>
<input type="submit" value=" OK ">
</p>
</form>
On Nov 1, 3:27 am, Milan Krejci <r...@n0spam.mail.cz> wrote:
> while(list($key,$value) = each($_FILES["file"]["name"]))
> {
> if(!empty($value)){
> $filename = $value;
> $add = "upimg/$filename";
> echo $_FILES["file"]["tmp_name"][$key];
> $error=copy($_FILES["file"]["tmp_name"][$key], $add);
> if (!$error) $progressUploadingPhotos=false;
> $error=chmod($add,0777);
> if (!$error) $progressUploadingPhotos=false;
>
> } else $progressUploadingPhotos=false;
> }
>
> the thing is that the file is never saved. any ideas?
[Back to original message]
|