我有以下代码将文件上传到文件夹,具体取决于文件输入上传文件重命名,例如第一个输入 - 图片重命名为picture01.jpg,第五个输入 - 图片重命名为picture到picture01.jpg等...
<form action="upload_file.php" enctype="multipart/form-data" method="POST">
<p><input name="image01" type="file"> </p>
<p><input name="image02" type="file"> </p>
<p><input name="image03" type="file"> </p>
<p><input name="image04" type="file"> </p>
<p><input name="image05" type="file"> </p>
<input type="submit" value="Upload File">
</form>
这是upload_file.php -
<?php
$pic_names = array (
'01' => 'picture01',
'02' => 'picture02',
'03' => 'picture03',
'04' => 'picture04',
'05' => 'picture05',
);
$folder = "temp_images/";
if(preg_match("/(jpg|jpeg|png|gif|mp3|mp4)/",$ext)) {
// Picture01
$image_path01 = $folder . $pic_names['01'] . '.jpg';
move_uploaded_file($_FILES['image01']['tmp_name'], $image_path01);
// Picture02
$image_path02 = $folder . $pic_names['02'] . '.jpg';
move_uploaded_file($_FILES['image02']['tmp_name'], $image_path02);
// Picture03
$image_path03 = $folder . $pic_names['03'] . '.jpg';
move_uploaded_file($_FILES['image03']['tmp_name'], $image_path03);
// Picture04
$image_path04 = $folder . $pic_names['04'] . '.jpg';
move_uploaded_file($_FILES['image04']['tmp_name'], $image_path04);
// Picture05
$image_path05 = $folder . $pic_names['05'] . '.jpg';
move_uploaded_file($_FILES['image05']['tmp_name'], $image_path05);
echo 'Uploaded successfully!';
} else { echo 'Uploaded successfully!';}
?>
我想在上传文件之前检查一下,在上传jpg,gif或png之前检查是否存在以下任一扩展名。如果他们有另一个扩展,那么它不会上传并给出错误。
我设法找到的唯一解决方案是当输入名称=“图片”时输入=“file”&gt;具有相同的名称,例如在这种情况下名称。如何根据输入的不同名称为我的案例执行此操作?
我遇到了以下错误:PHP Parse错误:语法错误,意外T_ELSE
虽然我确信它不仅仅是一个解析错误。
有任何建议或帮助吗?
更新:我修改了代码,但它告诉我'错误的文件类型!'对于任何图像格式也是如此。
答案 0 :(得分:1)
错误在字符串echo 'Uploaded successfully!';{
位置和内容中。
1)}
和else
之间不允许任何陈述。
2)'Uploaded successfully!';{
包含{
,这是多余的。
更新:处理上传文件的一种非常好的方法。取决于OP的$ pic_names数组。
$pic_names = array (
'01' => 'picture01',
'02' => 'picture02',
'03' => 'picture03',
'04' => 'picture04',
'05' => 'picture05',
);
$allowed_mime = array(
'image/jpeg' => 'jpg',
'image/pjpeg' => 'jpg',
'image/gif' => 'gif',
'image/png' => 'png',
);
$upload_messages = array(
UPLOAD_ERR_OK => ' Uploaded successfully!',
UPLOAD_ERR_INI_SIZE => ' The uploaded file exceeds the maxumum size of ' . ini_get('upload_max_filesize') . '.',
UPLOAD_ERR_PARTIAL => ' The uploaded file was only partially uploaded.',
UPLOAD_ERR_NO_FILE => ' No file was uploaded',
UPLOAD_ERR_NO_TMP_DIR => ' Missing a temporary folder.',
UPLOAD_ERR_CANT_WRITE => ' Failed to write file to disk.',
UPLOAD_ERR_EXTENSION => ' A PHP extension stopped the file upload.',
100 => ' Wrong file type.',
);
$folder = "temp_images/";
$error_message = $success_message = '';
$upload_counter = 0;
foreach ($pic_names as $key => $value) {
if (isset($_FILES['image'.$key]) && !$_FILES['image'.$key]['error']) {
$file_mime = getFileMime($_FILES['image'.$key]['tmp_name']);
if ( in_array($file_mime, array_keys($allowed_mime)) ) {
$new_path = $folder . $value . '.' . $allowed_mime[$file_mime];
if ( !move_uploaded_file($_FILES['image'.$key]['tmp_name'], $new_path) ) {
$error_message .= ' Image' . $key . ':' . $upload_messages[UPLOAD_ERR_CANT_WRITE];
} else {
$upload_counter++;
}
} else {
$error_message .= ' Image' . $key . ':' . $upload_messages[100];
}
} else {
$error_message .= ' Image' . $key . ':' . ($_FILES['image'.$key]['error'] ? $upload_messages[$_FILES['image'.$key]['error']] : $upload_messages[UPLOAD_ERR_NO_FILE]);
}
}
if ( $upload_counter == count($pic_names) ) {
echo $upload_messages[UPLOAD_ERR_OK];
} else {
echo 'Loaded ' . $upload_counter . ' file' . ($upload_counter != 1 ? 's' : '') . '.' . $error_message;
}
function getFileMime ($file) {
if (PHP_VERSION >= '5.3.0') {
$finfo = new finfo(FILEINFO_MIME_TYPE);
return $finfo->file($file);
} else {
return mime_content_type($file);
}
}
答案 1 :(得分:0)
更改
echo 'Uploaded successfully!';{
else { echo 'Uploaded successfully!';}
到
echo 'Uploaded successfully!';
} else { echo 'Wrong file type';}