php写上传图片api,php – 图片上传/接收API

最简单的方法是将文件存储在服务器上的文件夹中.然后将文件的URL存储在MySQL数据库中,如果用户不知道文件位置,则拉出文件URL(假设您有登录功能).

例如:

(UPLOAD.PHP或用于将文件上载到服务器的脚本)

$connect_to_db = mysqli_connect('localhost', 'user', 'pass', 'db');

$user = $_POST['user'];

$allowedExts = array("gif", "jpeg", "jpg", "png");

$temp = explode(".", $_FILES["file"]["name"]);

$extension = end($temp);

if ((($_FILES["file"]["type"] == "image/gif")

|| ($_FILES["file"]["type"] == "image/jpeg")

|| ($_FILES["file"]["type"] == "image/jpg")

|| ($_FILES["file"]["type"] == "image/pjpeg")

|| ($_FILES["file"]["type"] == "image/x-png")

|| ($_FILES["file"]["type"] == "image/png"))

&& in_array($extension, $allowedExts)) {

if ($_FILES["file"]["error"] > 0) {

echo "Error: " . $_FILES["file"]["error"] . "
";

} else {

//Move the file to the uploads folder

move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);

//Get the File Location

$filelocation = 'http://yourdomain.com/uploads/'.$_FILES["file"]["name"];

//Get the File Size

$size = ($_FILES["file"]["size"]/1024).' kB';

//Save to your Database

mysqli_query($connect_to_db, "INSERT INTO images (user, filelocation, size) VALUES ('$user', '$filelocation', '$size')");

//Redirect to the confirmation page, and include the file location in the URL

header('Location: confirm.php?location='.$filelocation);

}

} else {

//File type was invalid, so throw up a red flag!

echo "Invalid File Type";

}

?>

现在你可以做的是在一个页面中创建一个表,并根据登录的人列出所有上传的文件(再次假设你使用这个功能).这将允许该人知道必须记录他们上传的所有文件.

以下示例是您使用Ajax发布数据,并返回JSON格式的数据,因此您可以让用户不必重新加载页面.

$connect_to_db = mysqli_connect('localhost', 'user', 'pass', 'db');

$user = $_POST['user'];

$allowedExts = array("gif", "jpeg", "jpg", "png");

$temp = explode(".", $_FILES["file"]["name"]);

$extension = end($temp);

if ((($_FILES["file"]["type"] == "image/gif")

|| ($_FILES["file"]["type"] == "image/jpeg")

|| ($_FILES["file"]["type"] == "image/jpg")

|| ($_FILES["file"]["type"] == "image/pjpeg")

|| ($_FILES["file"]["type"] == "image/x-png")

|| ($_FILES["file"]["type"] == "image/png"))

&& in_array($extension, $allowedExts)) {

if ($_FILES["file"]["error"] > 0) {

echo json_encode(array('status' => 'error', 'msg' => 'File could not be uploaded.'));

} else {

//Move the file to the uploads folder

move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);

//Get the File Location

$filelocation = 'http://yourdomain.com/uploads/'.$_FILES["file"]["name"];

//Get the File Size

$size = ($_FILES["file"]["size"]/1024).' kB';

//Save to your Database

mysqli_query($connect_to_db, "INSERT INTO images (user, filelocation, size) VALUES ('$user', '$filelocation', '$size')");

//Return the data in JSON format

echo json_encode(array('status' => 'success', 'data' => array('filelocation' => $filelocation, 'size' => $size)));

}

} else {

//File type was invalid, so throw up a red flag!

echo json_encode(array('status' => 'error', 'msg' => 'Invalid File Format'));

}

?>

如果您还想限制文件大小,您可以添加一行简单的代码来检查文件大小,如果它很大,则不会让它通过:

if ((($_FILES["file"]["type"] == "image/gif")

|| ($_FILES["file"]["type"] == "image/jpeg")

|| ($_FILES["file"]["type"] == "image/jpg")

|| ($_FILES["file"]["type"] == "image/pjpeg")

|| ($_FILES["file"]["type"] == "image/x-png")

|| ($_FILES["file"]["type"] == "image/png"))

&& ($_FILES["file"]["size"] < 20000) //Must be smaller than 20KB

&& in_array($extension, $allowedExts)) {

如果您需要更多帮助,请随时告诉我. 🙂

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值