我正在尝试使以下 php 文件上传脚本正常工作,我已经在所有变量上使用了 var_dump() 和 print_r 并且据我所知它应该可以工作。事实上,这是可行的......在我的 Windows 安装上,我目前正在运行 Ubuntu 12.04,由于某种原因,这在我的本地设置上不起作用,我不愿意现场试用,这可能是我的 /tmp/ 的问题吗?权限?
<?php
function upload() {
/* * * check if a file was uploaded ** */
if (is_uploaded_file($_FILES['images']['tmp_name']) && getimagesize($_FILES['images']['tmp_name']) != false) {
/* * * get the image info. ** */
$size = getimagesize($_FILES['images']['tmp_name']);
/* * * assign our variables ** */
$type = $size['mime'];
$imgfp = fopen($_FILES['images']['tmp_name'], 'rb');
$size = $size[3];
$name = $_FILES['images']['name'];
$maxsize = 99999999;
$target_path = "/uploads/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$currentDate = date("Y-m-d");
$target_path = $target_path . $currentDate . ' - ' . basename($_FILES['images']['name']);
/* * * check the file is less than the maximum file size ** */
if ($_FILES['images']['size'] < $maxsize) {
if (move_uploaded_file($_FILES['images']['tmp_name'], $target_path)) {
echo "The file " . basename($_FILES['images']['name']) .
" has been uploaded";
} else {
echo "<div class='error'>There was an error uploading the file, please <a href='../artworkGenerator'>try again!</a></div>";
}
} else {
/* * * throw an exception if image is not of type ** */
throw new Exception("File Size Error");
}
} else {
// if the file is not less than the maximum allowed, print an error
throw new Exception("Unsupported Image Format!");
}
print_r();
}
/* * * check if a file was submitted ** */
if (!isset($_FILES['images'])) {
echo '<p>Please select a file</p>';
} else {
try {
upload();
/* * * give praise and thanks to the php gods ** */
echo '<p>Thank you for submitting</p>';
} catch (Exception $e) {
echo '<h4>' . $e->getMessage() . '</h4>';
}
}
?>
上传表格:
<form id="uploadForm" enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="99999999" />
<label>Attach file<span class="required"> *</span></label><img style="display:none;float:right;position: relative;top: 45px;right: 5px;" class="logo" src="img/tick.png" alt="logo" />
<hr class="small"/>
<br/>
<input type="file" name="images" id="images" multiple />
<ul id="image-list">
</ul>
<br/><br/>
<hr style="clear:both">
<input class="btn btn-success save" type="submit" id="btn" value="Generate"/>
<div id="invisible" style="display:none;">
</div>
</form>