链接两个PHP上传表单?

时间:2011-08-05 15:27:19

标签: php forms file-upload

我创建了两个单独的PHP表单。一个用于上传视频(因此它接受.avi,.mp4,.ogv),然后另一个用于上传视频缩略图(因此它接受.png,.jpeg)。有什么方法可以链接这些,以便它们在各自的文件夹或任何东西中以相同的名称创建?我是否可以在视频提交后才显示视频缩略图?感谢。

这是PHP:

<?php
define ('MAX_FILE_SIZE', 220200960); //define a constant for the maximum upload size (200 MB)
if (array_key_exists('uploadvideo', $_POST)) {
define('UPLOAD_DIR', 'videos/'); // define constant for upload folder
$file = str_replace(' ', '_', $_FILES['video']['name']); //replace any spaces with underscores, and at the same time assign to a simpler variable
$max = number_format(MAX_FILE_SIZE/1048576, 210). 'MB'; //convert the maximum size to MB
$permitted = array('video/x-msvideo','video/mp4','application/ogg');
$sizeOK = false; //begin by assuming the file is unacceptable
$typeOK = false;
if ($_FILES['video']['size'] > 0 && $_FILES['video']['size'] <= MAX_FILE_SIZE) {
$sizeOK = true;
} //check that file is within the permitted size

foreach ($permitted as $type) {
  if ($type == $_FILES['video']['type']) {
    $typeOK = true;
    break;
    }
  }

if ($sizeOK && $typeOK) {
switch($_FILES['video']['error']) {
  case 0: //move the file to the upload folder and rename it
  $success = move_uploaded_file($_FILES['video']['tmp_name'], UPLOAD_DIR.$file);
  if ($success) {
    $result ="$file uploaded successfully";
    }
  else {
    $result = "Error uploading $file. Please try again.";
    }
  break;
 case 3:
    $result = "Error uploading $file. Please try again.";
 default:
    $result = "Sysstem error uploading $file. Contact webmaster.";
 }
}
elseif ($_FILES['video']['error'] == 4) {
    $result = 'No file selected';
    }
else {
    $result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: .avi, .mp4, .ogv.";
    }
}
?>

<?php
define ('MAX_FILE_SIZE', 10485760); //define a constant for the maximum upload size (200 MB)
if (array_key_exists('uploadthumb', $_POST)) {
define('UPLOAD_DIR', 'thumbs/'); // define constant for upload folder
$file = str_replace(' ', '_', $_FILES['thumb']['name']); //replace any spaces with underscores, and at the same time assign to a simpler variable
$max = number_format(MAX_FILE_SIZE/1048576, 10). 'MB'; //convert the maximum size to MB
$permittedthumb = array('image/jpeg','image/pjpeg','image/png', 'image/x-png');
$sizeOK = false; //begin by assuming the file is unacceptable
$typeOK = false;
if ($_FILES['thumb']['size'] > 0 && $_FILES['thumb']['size'] <= MAX_FILE_SIZE) {
$sizeOK = true;
} //check that file is within the permitted size

foreach ($permittedthumb as $type) {
  if ($type == $_FILES['thumb']['type']) {
    $typeOK = true;
    break;
    }
  }

if ($sizeOK && $typeOK) {
switch($_FILES['thumb']['error']) {
  case 0: //move the file to the upload folder and rename it
  $successthumb = move_uploaded_file($_FILES['thumb']['tmp_name'], UPLOAD_DIR.$file);
  if ($successthumb) {
    $resultthumb ="$file uploaded successfully";
    }
  else {
    $resultthumb = "Error uploading $file. Please try again.";
    }
  break;
 case 3:
    $resultthumb = "Error uploading $file. Please try again.";
 default:
    $resultthumb = "Sysstem error uploading $file. Contact webmaster.";
 }
}
elseif ($_FILES['thumb']['error'] == 4) {
    $resultthumb = 'No file selected';
    }
else {
    $resultthumb = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: .avi, .mp4, .ogv.";
    }
}
?>

这是HTML:

<h3 class="titlehdrblue">Upload Video</h3>
<br></br>
<?php
if (isset($result)) {
echo "<p><strong>$result</strong></p><br></br>";
  }
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadVideo" id="uploadVideo">
<p>
  <label for ="video">Upload video:</label>
  <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
  <input type="file" name="video" id="video" />
</p>
<br></br>
<p>
  <input type="submit" name="uploadvideo" id="uploadvideo" value="Upload Video" />
</p>
</form>

<br></br>
<br></br>
<h3 class="titlehdrblue">Upload Video Thumbnail</h3>
<br></br>
<?php
if (isset($resultthumb)) {
echo "<p><strong>$resultthumb</strong></p><br></br>";
  }
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadThumb" id="uploadThumb">
<p>
  <label for ="thumb">Upload thumbnail:</label>
  <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
  <input type="file" name="thumb" id="thumb" />
</p>
<br></br>
<p>
  <input type="submit" name="uploadthumb" id="uploadthumb" value="Upload Thumbnail" />
</p>
</form>

1 个答案:

答案 0 :(得分:0)

只需将缩略图输入放在视频上传表单中,您就可以同时收到这两个