【发布时间】:2018-01-12 11:41:41
【问题描述】:
我的脚本正在工作,因为它在 HTML div 中显示上传和裁剪的图像。
将图片上传到PHP文件夹中并在mysql数据库中有图片的步骤是什么?
裁剪框:https://github.com/hongkhanh/cropbox
<?php
// Image avatar update
if (isset($_POST['submitavatar'])){
$name = $_FILES["fileToUpload"]["name"];
$name = mt_rand(100000, 999999).$name;
$uploadtmp = $_FILES["fileToUpload"]["tmp_name"];
$target_dir = "avatarprofile/";
$target_file = $target_dir . basename($name);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$name = preg_replace("#[^a-z0-9.]#i", "", $name);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
$updateavatar = $name;
$sql = "UPDATE users SET avatar = '$updateavatar' WHERE userID = $user";
$result = $con->query($sql);
$img= $_SESSION["avatar"];
unlink("./avatarprofile/$img");
header('Location: profile-settings-avatar.php');
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
<div class="container">
<div class="imageBox">
<div class="thumbBox"></div>
<div class="spinner" style="display: none">Loading...</div>
</div>
<div class="action">
<input type="file" name="fileToUpload" id="file" style="float:left; width: 250px">
<input type="button" id="btnCrop" value="Crop" style="float: right">
<input type="button" id="btnZoomIn" value="+" style="float: right">
<input type="button" id="btnZoomOut" value="-" style="float: right">
</div>
<form action="profile-settings-avatar.php" method="POST" enctype="multipart/form-data">
<div type="file" class="cropped">
</div>
<input type="submit" id="uploadimagebutton" value="Upload" name="submitavatar">
</form>
</div>
链接javasript:https://github.com/hongkhanh/cropbox
【问题讨论】:
-
要将图片路径保存到数据库需要建立连接(建议使用
\PDO)。旁注:您在header('Location: ...之后忘记了exit;。发送标头不会停止执行脚本。 -
这不是php的问题。如果我输入表格enctype =“multipart / form-data,所有内容都会定期保存在文件夹中并保存在mysql中,但我需要一个更可爱的脚本来编辑图像并在javascript中工作。”脚本将剪切的图片保存在 class= "cropped" 图像中。它以 img src 格式保存...如何将此图像从 PHP 保存到文件夹和数据库?请帮助这个我迫切需要它我整天都在等待答案。链接脚本已启动...
-
我已经稍微改进了你的问题。
标签: javascript php image image-uploading crop