我终于取得了一些进展。现在,当我上传 2 张图片时,它会被放入我的数组中。我附上了我的vardumb。但是文件实际上并没有上传?有任何想法吗?
array(5) {
["name"]=>
array(2) {
[0]=>
string(11) "1bTUWI3.jpg"
[1]=>
string(11) "1T1NBmd.jpg"
}
["type"]=>
array(2) {
[0]=>
string(10) "image/jpeg"
[1]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(2) {
[0]=>
string(14) "/tmp/phpgB2bAe"
[1]=>
string(14) "/tmp/phpg8ZZk1"
}
["error"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(0)
}
["size"]=>
array(2) {
[0]=>
int(671869)
[1]=>
int(352029)
}
}
function uploadFile()
}
$files = array();
$fdata = $_FILES['userfile'];
if (is_array($fdata["name"])){
for ($i = 0; $i < count($fdata['name']); ++$i) {
$files[] = array(
'name' => $fdata['name'][$i],
'tmp_name' => $fdata['tmp_name'][$i],
'size' => $fdata['size'][$i],
'filetype' => $fdata['type'][$i],
);
}
echo '<pre>';
var_dump($fdata);
echo '</pre>';
$username=$_SESSION['name'];
$alt=mysqli_real_escape_string($conn, $_POST['alt']);
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["userfiles"]["name"]));
if((($fdata['type'] == "image/gif")
||($fdata['type']=="image/jpeg")
||($fdata['type']=="image/png")
||($fdata['type']=="image/pjpeg")
&& in_array($extension, $allowedExts)))
{
$fp = fopen($tmpName, 'r');
$content =fread($fp, filesize($tmpName));
$SourceImage = imagecreatefromstring($content);
$SourceWidth = imagesx($SourceImage);
$SourceHeight=imagesy($SourceImage);
$DestWidth=100;
$DestHeight=130;
if ($SourceHeight> $SourceWidth)
{$ratio = $DestHeight / $SourceHeight;
$newHeight = $DestHeight;
$newWidth = $sourceWidth * $ratio;
}
else
{
$ratio = $DestWidth / $SourceWidth;
$newWidth = $DestWidth;
$newHeight = $SourceHeight * $ratio;
}
$DestinationImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($DestinationImage, $SourceImage, 0,0,0,0,$DestWidth, $DestHeight, $SourceHeight, $SourceWidth);
ob_start();
imagejpeg($DestinationImage);
$BinaryThumbnail = ob_get_contents();
ob_end_clean();
$thumb = addslashes($BinaryThumbnail);
$content = addslashes($content);
fclose($fp);
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
mysqli_query($conn, "INSERT INTO files (username, name, size, content, type, link, alt, thumbnail) VALUES ('$username', '$fileName', '$fileSize', '$content', '$fileType', 1, '$alt', '$thumb')") or die('Error, query failed');
echo "<script>alert('The file has been uploaded');location.replace('uploaded.php');</script>";
}else{
echo "<script>alert('Please upload an image');location.replace('upload.php');</script>";
}
}
}
我仍然陷入我的回声错误。
我的表格看起来像这样。
<h1>Upload a file</h1>
<form method="post" enctype="multipart/form-data" action="process.php">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<label>Upload File:
<input name="userfile" type="file" id="userfile"></label>
<br>
<label>Alt Text: <input name="alt" type="text"></label>
<input name="UploadFile" type="submit" />
</form>
我的 vardump 看起来像这样
array(1) {
["userfile"]=>
array(5) {
["name"]=>
array(1) {
[0]=>
string(17) "Desert - Copy.jpg"
}
["type"]=>
array(1) {
[0]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(1) {
[0]=>
string(14) "/tmp/phpIpCoQ3"
}
["error"]=>
array(1) {
[0]=>
int(0)
}
["size"]=>
array(1) {
[0]=>
int(845941)
}
}
}