我关注了 tutorial在线构建多文件 uploader ,添加评论, 当上传的文件移动到上传文件夹并且错误消息有效时,一切都成功运行。然而,虽然我可以 print_r 显示路径和文件名的 $uploaded 数组,但我真正想做的是在 html < img/> 标记中回显此路径,如下所示:
if($uploaded) {
$name = $uploaded['filename']['name'];
move_uploaded_file($uploaded['filename']['tmp_name'], $name);
echo "<img src='" . $name . "' />";
}
并将图像显示在页面上。这是文件。
上传.php
<?php
if(!empty($_FILES['files']['name'][0])) {
$files = $_FILES['files'];
$uploaded = array();
$failed = array();
$allowed = array('png', 'jpg');
//Loops files and collects file name
foreach($files['name'] as $position => $file_name) {
//Collects temporary location size and error of looped file
$file_tmp = $files['tmp_name'] [$position];
$file_size = $files['size'][$position];
$file_error = $files['error'][$position];
//Collects file extention
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
//References allowed extensions array
if(in_array($file_ext, $allowed)) {
// checks for errors otherwise else statements
if($file_error === 0) {
// checks file size otherwise else statements
if($file_size <= 2097152 ) {
// uniqid generates new file based on time stamp name
// defines server file destination folder
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = 'uploads/' . $file_name_new;
//new destination for temporary file
if(move_uploaded_file($file_tmp, $file_destination)) {
$uploaded[$position] = $file_destination;
} else {
$failed[$position] = "[{$file_name}] failed to upload.";
}
} else {
$failed[$position] = "[{$file_name}] is too large.";
}
} else {
$failed[$position] = "[{$file_name}] errored with code {$file_error}.";
}
} else {
$failed[$position] = "[{$file_name}] file extension '{$file_ext}' is not allowed.";
}
}
//prints $uploaded array if successful
if(!empty($uploaded)) {
print_r($uploaded);
}
//prints $failed else statments otherwise
if (!empty($failed)) {
print_r($failed);
}
}
?>
表单索引.php
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>untitled</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="">
<!-- Date: 2014-08-02 -->
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="files[ ]" multiple>
<input type="submit" value="Upload">
</form>
</body>
</html>
最佳答案
好的,在研究了您的代码后,我得到了以下对我来说可以正常工作的内容。
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>untitled</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="">
<!-- Date: 2014-08-02 -->
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="files[ ]" multiple>
<input type="submit" value="Upload">
</form>
</body>
</html>
上传.php
<?php
//
if(!empty($_FILES['files']['name'][0])) {
$files = $_FILES['files'];
$uploaded = array();
$failed = array();
$allowed = array('png', 'jpg');
//Loops files and collects file name
foreach($files['name'] as $position => $file_name) {
//Collects temporary location size and error of looped file
$file_tmp = $files['tmp_name'] [$position];
$file_size = $files['size'][$position];
$file_error = $files['error'][$position];
//Collects file extention
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
//References allowed extensions arry
if(in_array($file_ext, $allowed)) {
// checks for errors otherwise else statments
if($file_error === 0) {
// checks file size otherwise else statements
if($file_size <= 2097152 ) {
// uniqid generates new file based on time stamp name
// defines server file destination folder
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = 'uploads/' . $file_name_new;
//new destination for temporary file
if(move_uploaded_file($file_tmp, $file_destination)) {
$uploaded[$position] = $file_destination;
} else {
$failed[$position] = "[{$file_name}] failed to upload.";
}
} else {
$failed[$position] = "[{$file_name}] is too large.";
}
} else {
$failed[$position] = "[{$file_name}] errored with code {$file_error}.";
}
} else {
$failed[$position] = "[{$file_name}] file extension '{$file_ext}' is not allowed.";
}
}
//prints $uploaded arry if successful
if(!empty($uploaded)) {
}
//prints $failed else statments otherwise
if (!empty($failed)) {
//print_r($failed);
}
if($uploaded) {
foreach ($uploaded as $image)
{
echo "<img src='".$image."' />";
}
}
}
?>
关于php - 在 IMG 标签中回显图像路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25108818/