I have created two separate PHP forms.. One to upload a video (so it accepts .avi, .mp4, .ogv), and then another to upload the video thumbnail (so it accepts .pn开发者_如何学Pythong, .jpeg). Is there any way I can link these so that they are created under the same name in their respective folders or anything? And can I make the video thumbnail form only show up after the video one has been submitted? Thanks.
Here's the 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.";
}
}
?>
And here's the 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>
Just put that thumbnail input in your video-upload form and you'll receive both at the same time
精彩评论