在php问题中自动上传图片

时间:2014-09-27 21:07:04

标签: javascript php

SAy,我有将图像上传到服务器和显示器的脚本。它也工作正常。但是,我不想手动上传图像到表单,只需双击图像,它将以与我们相同的方式上传可以选择谷歌“搜索此图像的颜色”或只是一个按钮使用jquery点击图像将被上传。任何想法,我的代码我用来上传文件:

<?php
$target_path = "./";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)){
echo "<img src='".$target_path."' class='healthfood' />";
}
?>      

<body>
<form enctype="multipart/form-data" action="<?php $_PHP_SELF ?>" method="POST">
<input type="hidden" name="file" value="upload file" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

2 个答案:

答案 0 :(得分:0)

HTML

   <form enctype="multipart/form-data" action="<?php $_PHP_SELF ?>" method="POST">
         <input type="hidden" name="file" value="upload file" />
    Choose a file to upload: <input id="file" name="uploadedfile" type="file" /><br />
         <input type="submit" value="Upload File" />
    </form>

JAVASCRIPT

// brefore this don't froget to add Jquery Libary and if alredy exist don't repeat 
$(document).ready(function(){
    $("#file").on("change",function(){
       // alert("a");
        $(this).parent("form").submit();
    });
});

答案 1 :(得分:-2)

更简单:

<form id=form1 action=whatever>
  <input type=file id=x /><br/>
  <input type=button onclick='form1.submit();' value='Click This to Upload'/>
</form>