如何在 php 中将文件从本地计算机发送到我的服务器?
How to send file from local computer to my server in php?
我想上传一张图片文件(示例 - "C:\Users\Public\Pictures.png")到我的 ftp 服务器(ftp://srict.96.lt)。
我有以下 HTML 代码,允许用户从本地计算机 select 文件。
<html>
<body>
<form action="imgup.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
现在在 imgup.php 我尝试了以下代码,但它说 "move_uploaded_file(/Hydrangeas.jpg): failed to open stream: Permission denied in /home/u704250527/public_html/imgup.php on line 21"
<?php
$target_dir = "public_html/"; /* I also tried for "/" "root/public_html/" */
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if ($uploadOk == 0)
{
echo "Sorry, your file was not uploaded.";
}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
}
else
{
echo "Sorry, there was an error uploading your file.";
}
}
?>
我需要在用户点击上传按钮时上传文件。
我也尝试在 php 中使用 ftp 这也给了我同样的错误无法打开流。
<?php
// connect and login to FTP server
$ftp_server = "ftp.srict.96.lt";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$ftp_username="user";
$ftp_userpass="pass";
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "D:\Picture1.png";
$fp = fopen($file,"r");
if (ftp_fput($ftp_conn, "1.png", $fp, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
ftp_close($ftp_conn);
?>
终于找到问题所在了。必须选择上传的文件并使用 $_FILES['filetoupload']['tmp_name'];
添加
这是下面的完整代码。
对于HTML:
<!DOCTYPE html>
<html>
<body>
<form action="imgup.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
一旦用户点击上传,下面的 php 脚本就会执行,魔法就会发生。
<?php
// connect and login to FTP server
$ftp_server = "ftp.my.server.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$ftp_username="my_username";
$ftp_userpass="my_password";
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
var_dump($_FILES);
if (ftp_put($ftp_conn, "1.png",$_FILES['fileToUpload']['tmp_name'], FTP_BINARY))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>
这是这里的重要行 这表示使用 FTP_BINARY 模式上传文件,该模式最适合图像和文档文件。另外注意"tmp_name"上传
ftp_put($ftp_conn, "1.png",$_FILES['fileToUpload']['tmp_name'], FTP_BINARY)
我想上传一张图片文件(示例 - "C:\Users\Public\Pictures.png")到我的 ftp 服务器(ftp://srict.96.lt)。
我有以下 HTML 代码,允许用户从本地计算机 select 文件。
<html>
<body>
<form action="imgup.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
现在在 imgup.php 我尝试了以下代码,但它说 "move_uploaded_file(/Hydrangeas.jpg): failed to open stream: Permission denied in /home/u704250527/public_html/imgup.php on line 21"
<?php
$target_dir = "public_html/"; /* I also tried for "/" "root/public_html/" */
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if ($uploadOk == 0)
{
echo "Sorry, your file was not uploaded.";
}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
}
else
{
echo "Sorry, there was an error uploading your file.";
}
}
?>
我需要在用户点击上传按钮时上传文件。 我也尝试在 php 中使用 ftp 这也给了我同样的错误无法打开流。
<?php
// connect and login to FTP server
$ftp_server = "ftp.srict.96.lt";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$ftp_username="user";
$ftp_userpass="pass";
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "D:\Picture1.png";
$fp = fopen($file,"r");
if (ftp_fput($ftp_conn, "1.png", $fp, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
ftp_close($ftp_conn);
?>
终于找到问题所在了。必须选择上传的文件并使用 $_FILES['filetoupload']['tmp_name'];
添加这是下面的完整代码。
对于HTML:
<!DOCTYPE html>
<html>
<body>
<form action="imgup.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
一旦用户点击上传,下面的 php 脚本就会执行,魔法就会发生。
<?php
// connect and login to FTP server
$ftp_server = "ftp.my.server.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$ftp_username="my_username";
$ftp_userpass="my_password";
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
var_dump($_FILES);
if (ftp_put($ftp_conn, "1.png",$_FILES['fileToUpload']['tmp_name'], FTP_BINARY))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>
这是这里的重要行 这表示使用 FTP_BINARY 模式上传文件,该模式最适合图像和文档文件。另外注意"tmp_name"上传
ftp_put($ftp_conn, "1.png",$_FILES['fileToUpload']['tmp_name'], FTP_BINARY)