Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ajax-php上载多个文件并显示以前上载的文件_Php_Ajax - Fatal编程技术网

ajax-php上载多个文件并显示以前上载的文件

ajax-php上载多个文件并显示以前上载的文件,php,ajax,Php,Ajax,Html表单 <form enctype="multipart/form-data" method="post"> <input name="files[]" type="file" id="upload_file" /><br/> <input type="button" id="upload" value="Upload File" /> </form> 然后用php处理,就像这里实际的代码有点不同 if ( !empty

Html表单

 <form enctype="multipart/form-data" method="post">
 <input name="files[]" type="file" id="upload_file" /><br/>
 <input type="button" id="upload" value="Upload File" />
 </form>
然后用php处理,就像这里实际的代码有点不同

if ( !empty( $_FILES['files']['name'] ) ) {
$target_path = '../images/';
$ext = explode('.', basename( $_FILES['files']['name'] ) );
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext)-1]; 
$val_img = substr( $target_path , 10 );

//Here code to display uploaded image

if(move_uploaded_file($_FILES['files']['tmp_name'][$i], $target_path)) {
echo '<a href=""><img src="../images/'. $val_img. '" ></a> ';
}

}
问题是在每个$'upload_file.changefunction{上载一个新文件并回显';只回显新上载的文件。但是如何回显以前上载的文件

目前,您认为需要会话或从mysql获取。 像foreach会话或mysql数据

回声'

我的意思是:用户打开页面,点击上传;上传第一,第二等文件,看到所有上传的文件现在只能看到最后上传的文件


有更好的解决方案吗?

这是一个非常糟糕的代码。你只需假设上传永远不会失败,并且不对上传进行任何形式的验证。我将编写验证代码。目前的目标是显示所有上传的文件。关于验证,我正试图遵循以下建议,
if ( !empty( $_FILES['files']['name'] ) ) {
$target_path = '../images/';
$ext = explode('.', basename( $_FILES['files']['name'] ) );
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext)-1]; 
$val_img = substr( $target_path , 10 );

//Here code to display uploaded image

if(move_uploaded_file($_FILES['files']['tmp_name'][$i], $target_path)) {
echo '<a href=""><img src="../images/'. $val_img. '" ></a> ';
}

}