使用 PHP 和 JavaScript 上传文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18698325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:55:06  来源:igfitidea点击:

Upload a file Using PHP and JavaScript

phpjavascriptmysql

提问by Ramesh Yadav

I selected a image using:

我使用以下方法选择了一个图像:

<input type="file" id="pimg" name="pimg" accept='image/*'/>  

My javascript code:

我的javascript代码:

p_img =document.getElementById("pimg").value;  
param= 'pn='+p_img;  
xmlhttp.open("GET","add_prod.php?"+param,false);  
xmlhttp.send();  

My php code:

我的PHP代码:

p_img=$_GET['img'];
$con = mysqli_connect('localhost', 'admin', 'admin', 'products');
$sql="INSERT INTO prod (img) VALUES ('$p_img')";
if (!mysqli_query($con,$sql))
{
    die('Error: ' . mysqli_error($con));
}  

This will store only the name of the file. But i want to copy the file from pc to directory. Its necessary to use Javascript as I'm using complete add product to pass values using AJAX

这将仅存储文件的名称。但我想将文件从 pc 复制到目录。使用 Javascript 是必要的,因为我正在使用完整的添加产品来使用 AJAX 传递值

回答by Dilip Godhani

Create index.html file

创建 index.html 文件

<!DOCTYPE html>
<html>
<body>
    <form action="upload_file.php" method="post" enctype="multipart/form-data">
        <label for="file">Filename:</label>
        <input type="file" name="file" id="file"><br>
        <input type="submit" name="submit" value="Submit">
    </form>
</body>
</html>

Create Php file upload_file.php

创建 PHP 文件 upload_file.php

<?php

if ($_FILES["file"]["error"] > 0) {
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
}

?>

回答by q0re

Use POST(form) to send data to the php file.

使用POST(form) 将数据发送到 php 文件。

With $_FILES["pimg"]["tmp_name"] you can move the uploaded file (with the php function move_uploaded_fileto you webserver.

使用 $_FILES["pimg"]["tmp_name"] 您可以移动上传的文件(使用 php 功能move_uploaded_file到您的网络服务器。

Link to PHP Function http://php.net/manual/de/function.move-uploaded-file.php

链接到 PHP 函数 http://php.net/manual/de/function.move-uploaded-file.php

回答by JoyGuru

You can upload the file without page refresh using simple JavaScript and PHP. Using JavaScript, the file would be passed to the PHP file and using move_uploaded_file()function file would be uploaded to the server.

您可以使用简单的 JavaScript 和 PHP 上传文件而无需刷新页面。使用 JavaScript,文件将被传递到 PHP 文件,并使用move_uploaded_file()函数文件将上传到服务器。

The live demo and source code would found from here - Upload file using JavaScript and PHP

现场演示和源代码可以从这里找到 -使用 JavaScript 和 PHP 上传文件

回答by K55555

xmlhttp.open("GET","add_prod.php?"+param,false);  

i think open method's parameter must contain true.

我认为 open 方法的参数必须包含 true。