我不知道为什么,但我网站上的文件上传对于在移动设备上使用它的一半用户不起作用。它适用于桌面和一些移动设备。该脚本运行并表示已输入文件并准备上传,但之后无法上传任何内容。
NetUpload();正在调用,这意味着服务器认为文件已上传。再一次,这个问题只发生在移动设备上,它在我的iPhone 6s +(Safari),我朋友的Galaxy Note 7(默认浏览器)上失败了,但是在我的另一个朋友的三星Galaxy 6上工作了(铬)。
上传页面:
<script>
$(function () {
$('#upload-button').click(function () {
$('#file-data').click();
});
$('#file-data').change(function () {
document.getElementById("upload-button").style.visibility = "hidden";
document.getElementById("upload-button").style.display = "none";
document.getElementById("submit-image").style.visibility = "visible";
document.getElementById("submit-image").style.display = "block";
document.getElementById("post-tags").style.visibility = "visible";
document.getElementById("post-tags").style.display = "block";
});
});
</script>
</head>
<body>
<?php Repeats::Navigation(); ?>
<div class="container">
<div class="form-container">
<div class="message">We're aware of the issue some users are having with uploading on their mobile devices, we're currently looking into this.</div> <br />
<form action="api.php?call=upload&arg1=<?php print $_SESSION["postkey"]; ?>&arg2=browser" method="POST" enctype="multipart/form-data">
<input type="button" id="upload-button" name="upload-button" value="Choose Image">
<input type="text" class="no-round" id="post-tags" name="posttags" placeholder="Post tags (seperated by commas)" style="display: none; visibility: hidden;"/>
<input type="file" name="file" id="file-data" accept="image/*" style="visibility: hidden; display: none;">
<input type="submit" name="submit-image" id="submit-image" style="visibility: hidden; display: none;" value="Upload">
</form>
</div>
</div>
</body>
上传脚本:
// There is a file being uploaded.
if (isset($_FILES['file']))
{
require('inc/classes/Compression.php');
$error = '';
$query = $db->connection->query("SELECT * FROM `pictures`");
$imageID = $query->rowCount() + 1;
$target_file = 'images/'.basename($_FILES["file"]["name"]);
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
$target_file = "images/".$imageID.'.'.$imageFileType;
if (empty($_FILES["file"]["tmp_name"]))
{
$error = 'You did not choose an image to upload.';
$uploadOk = 0;
}
else
{
// Make sure what is being uploaded is an image.
if(getimagesize($_FILES["file"]["tmp_name"]) !== false)
$uploadOk = 1;
else
$error = 'This is not an image.';
// If the file already exists, something went wrong.
if (file_exists($target_file))
$error = 'Something went wrong, please try again later!';
// Check if the file is within the size limits.
if ($_FILES["file"]["size"] > Settings::$settings["maxsize"] || $_FILES["file"]["size"] < Settings::$settings["minsize"])
$error = 'File size is invalid.';
// Only accept these image formats.
if(strtolower($imageFileType) != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "gifv" )
$error = 'Sorry, only JPG, JPEG, PNG & GIF files are supported.';
if ($error != '')
{
print json_encode(array(
"Status" => "error",
"Message" => $error
));
exit;
}
// Use PNG compression if we're off the local server.
if ($imageFileType == "png" && Settings::$settings["local"] == "no")
{
file_put_contents($target_file, compress_png($_FILES['file']['tmp_name']));
NewUpload(UidFromPostkey($_GET['arg1']), $imageFileType, $imageID, $_POST['posttags']);
print json_encode(array(
"Status" => "success",
"Message" => 'Your upload is complete.'
));
if (isset($_GET['arg2']) && $_GET['arg2'] == 'browser')
header('Location: user.php');
}
// We're either on the local server or our file is not a PNG.
else if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file))
{
NewUpload(UidFromPostkey($_GET['arg1']), $imageFileType, $imageID, $_POST['posttags']);
print json_encode(array(
"Status" => "success",
"Message" => 'Your upload is complete.'
));
if (isset($_GET['arg2']) && $_GET['arg2'] == 'browser')
header('Location: user.php');
}
}
}
else
{
print json_encode(array(
"Status" => "error",
"Message" => "no file selected."
));
}
Compression.php
<?php
function compress_png($path_to_png_file, $max_quality = 90)
{
if (!file_exists($path_to_png_file)) {
throw new Exception("File does not exist: $path_to_png_file");
}
$min_quality = 60;
// '-' makes it use stdout, required to save to $compressed_png_content variable
// '<' makes it read from the given file path
// escapeshellarg() makes this safe to use with any path
$compressed_png_content = shell_exec("pngquant --quality=$min_quality-$max_quality - < ".escapeshellarg( $path_to_png_file));
if (!$compressed_png_content) {
throw new Exception("Conversion to compressed PNG failed. Is pngquant 1.8+ installed on the server?");
}
return $compressed_png_content;
}