Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.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
Php 一次点击即可上传html文件(图像)_Php_Html_File Upload - Fatal编程技术网

Php 一次点击即可上传html文件(图像)

Php 一次点击即可上传html文件(图像),php,html,file-upload,Php,Html,File Upload,我想在一次点击中上传一个文件,因此我尝试在一次点击中组合两个点击事件,但是$\u file变量不会加载图像,下面是我的代码: <form target='_self' action='upload.php' method='post' enctype='multipart/form-data'> <div class = 'testocentrato'> <input style='display:none' type='fi

我想在一次点击中上传一个文件,因此我尝试在一次点击中组合两个点击事件,但是
$\u file
变量不会加载图像,下面是我的代码:

    <form target='_self' action='upload.php' method='post' enctype='multipart/form-data'>
        <div class = 'testocentrato'>
        <input style='display:none' type='file' accept='.jpg' name='file' id='file'/>
        <input style='display:none' type='submit' id='caricaimmagine' name='caricaimmagine' />
        <input class='inputfile' type='button' value='Scegli file da PC' onclick='document.getElementById('file').click(); document.getElementById('caricaimmagine').click();' />
        <input style='display:none' type='submit' />
        <input class='inputfile' type='submit'  name='eliminaimmagine' onclick='document.getElementById('eliminaimmagine').click();' value='".$lang['TASTO_ELIMINA_FOTO']."' />
        <input type='hidden' name='id_utente' value='".$user['id']."' />        
   </form>

因为您在分区文件之前直接单击提交。 去除 document.getElementById('caricalmagine')。单击();
您需要手动单击该按钮。

因为您在分区文件之前直接单击提交。 去除 document.getElementById('caricalmagine')。单击();
您需要手动单击该按钮。

使用ajax一次单击即可上载图像,请选中。 这是我的html代码,我使用了标签标签,所以当我点击上传图标时,选择文件窗口就会出现

<form method="post"  enctype="multipart/form-data">
    <label class='foto' for="imgid">
            <img id="photo" src="images/photo.png"> 
            <p>Foto</p>
         </label>

        <input type="file" value="chosen File" id="imgid" name="img" accept="image/*">
                
    <img class="previewimg" src="" width="100" height="100">
    <span id="showImagePath" ></span>

    <button class="btn btn-primary" id="toPost">Post</button>
            
</form>
我的Jquery: 我使用setInterval()函数使脚本运行,直到选择了上载图像的方式,并且可以预览名称和图像

$("#photo").click(function(){
                setInterval(function(){
                    var fd = new FormData();
                    var files = $('#imgid')[0].files;
                   // Check file selected or not
                    if(files.length > 0 ){
                       $(".previewimg").show();
                       fd.append('file',files[0]);

                       $.ajax({
                          url: 'action.php?action=postcontentimage',
                          type: 'post',
                          data:fd,
                          contentType: false,
                          processData: false,
                          success: function(result){
                              var name ="";
                                if(result != 0){
                                    $(".previewimg").attr("src", result).show();
                                    for(var i=0; i < result.length;i++){
                                        if(i>8){
                                            name += result[i];
                                        }
                                        $("#showImagePath").html(name);
                                        clearInterval();
                                    }
                                    
                                }
                            }
                        })
                    }else{
                        alert("no image has been selected");
                    }
                    
                }, 2000);
                
            })
$(“#照片”)。单击(函数(){
setInterval(函数(){
var fd=新FormData();
var files=$('#imgid')[0]。文件;
//检查是否选择了文件
如果(files.length>0){
$(“.previewimg”).show();
fd.append('file',files[0]);
$.ajax({
url:'action.php?action=postcontentimage',
键入:“post”,
数据:fd,
contentType:false,
processData:false,
成功:功能(结果){
var name=“”;
如果(结果!=0){
$(“.previewimg”).attr(“src”,result).show();
for(变量i=0;i8){
名称+=结果[i];
}
$(“#showImagePath”).html(名称);
clearInterval();
}
}
}
})
}否则{
警报(“未选择图像”);
}
}, 2000);
})
我的PHP:

<?php

if(isset($_FILES['file']['name'])){

   /* Getting file name */
   $filename = $_FILES['file']['name'];

   /* Location */
   $location = "uploads/".$filename;
   $imageFileType = pathinfo($location,PATHINFO_EXTENSION);
   $imageFileType = strtolower($imageFileType);

   /* Valid extensions */
   $valid_extensions = array("jpg","jpeg","png");

   $response = 0;
   /* Check file extension */
   if(in_array(strtolower($imageFileType), $valid_extensions)) {
      /* Upload file */
      if(move_uploaded_file($_FILES['file']['tmp_name'],$location)){
         $response = $location;
      }
   }

   echo $response;
   exit;
}else{

    echo 0;
}
?>

使用ajax,只需单击一下即可上载图像,请选中。 这是我的html代码,我使用了标签标签,所以当我点击上传图标时,选择文件窗口就会出现

<form method="post"  enctype="multipart/form-data">
    <label class='foto' for="imgid">
            <img id="photo" src="images/photo.png"> 
            <p>Foto</p>
         </label>

        <input type="file" value="chosen File" id="imgid" name="img" accept="image/*">
                
    <img class="previewimg" src="" width="100" height="100">
    <span id="showImagePath" ></span>

    <button class="btn btn-primary" id="toPost">Post</button>
            
</form>
我的Jquery: 我使用setInterval()函数使脚本运行,直到选择了上载图像的方式,并且可以预览名称和图像

$("#photo").click(function(){
                setInterval(function(){
                    var fd = new FormData();
                    var files = $('#imgid')[0].files;
                   // Check file selected or not
                    if(files.length > 0 ){
                       $(".previewimg").show();
                       fd.append('file',files[0]);

                       $.ajax({
                          url: 'action.php?action=postcontentimage',
                          type: 'post',
                          data:fd,
                          contentType: false,
                          processData: false,
                          success: function(result){
                              var name ="";
                                if(result != 0){
                                    $(".previewimg").attr("src", result).show();
                                    for(var i=0; i < result.length;i++){
                                        if(i>8){
                                            name += result[i];
                                        }
                                        $("#showImagePath").html(name);
                                        clearInterval();
                                    }
                                    
                                }
                            }
                        })
                    }else{
                        alert("no image has been selected");
                    }
                    
                }, 2000);
                
            })
$(“#照片”)。单击(函数(){
setInterval(函数(){
var fd=新FormData();
var files=$('#imgid')[0]。文件;
//检查是否选择了文件
如果(files.length>0){
$(“.previewimg”).show();
fd.append('file',files[0]);
$.ajax({
url:'action.php?action=postcontentimage',
键入:“post”,
数据:fd,
contentType:false,
processData:false,
成功:功能(结果){
var name=“”;
如果(结果!=0){
$(“.previewimg”).attr(“src”,result).show();
for(变量i=0;i8){
名称+=结果[i];
}
$(“#showImagePath”).html(名称);
clearInterval();
}
}
}
})
}否则{
警报(“未选择图像”);
}
}, 2000);
})
我的PHP:

<?php

if(isset($_FILES['file']['name'])){

   /* Getting file name */
   $filename = $_FILES['file']['name'];

   /* Location */
   $location = "uploads/".$filename;
   $imageFileType = pathinfo($location,PATHINFO_EXTENSION);
   $imageFileType = strtolower($imageFileType);

   /* Valid extensions */
   $valid_extensions = array("jpg","jpeg","png");

   $response = 0;
   /* Check file extension */
   if(in_array(strtolower($imageFileType), $valid_extensions)) {
      /* Upload file */
      if(move_uploaded_file($_FILES['file']['tmp_name'],$location)){
         $response = $location;
      }
   }

   echo $response;
   exit;
}else{

    echo 0;
}
?>


ok,…我的目标不是单击两次,但可能不可能。ok,…我的目标不是单击两次,但可能不可能。如果您可以格式化代码,这将对其他人有帮助。如果您可以格式化代码,这将对其他人有帮助。