Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form method="post" name="update_form" action="php/editpost" enctype="multipart/form-data">
- <div class="form-group">
- <label for="img_upload">Select image</label>
- <input type="file" name="img_upload" id="img_upload" class="form-control">
- </div>
- <div class="form-group">
- <input class="btn btn-success btn-lg" type="submit" value="Update" name="edit_submit" id="edit_submit">
- </div>
- </form>
- if ($_FILES["img_upload"]["name"]) {
- $target_dir = "uploads/";
- $target_file = $target_dir . basename($_FILES["img_upload"]["name"]);
- $imgsrc = $target_file;
- $uploadOk = 1;
- $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
- // Check if image file is a actual image and square
- $check = getimagesize($_FILES["img_upload"]["tmp_name"]);
- if($check !== false) {
- $width = $check[0];
- $height = $check[1];
- if ($width != $height) {
- $error = $error."<br />- The image is not square, please crop it and try again.";
- $uploadOk = 0;
- }
- } else {
- $error = $error."<br />- File is not an image.";
- $uploadOk = 0;
- }
- // Check if file already exists
- if (file_exists($target_file)) {
- chmod($target_file,0755); // Change the file permissions
- unlink($target_file); // remove the file
- }
- // Check file size
- if ($_FILES["img_upload"]["size"] > 1000000) {
- $error = $error."<br />- Your image is too large.";
- $uploadOk = 0;
- }
- // Allow certain file formats
- if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
- $error = $error."<br>- Only JPG, JPEG & PNG images are allowed.";
- $uploadOk = 0;
- }
- // Check if $uploadOk is set to 0 by an error
- if ($uploadOk == 0) {
- $fail .= "There were error(s) while uploading the image:<br />".$error;
- }
- // if everything is ok, try to upload file
- else {
- if (move_uploaded_file($_FILES["img_upload"]["tmp_name"], $target_file)) {
- $sql = mysqli_query($link,"UPDATE `posts` SET `imagesrc`='$imgsrc' WHERE `postid`='$postid' LIMIT 1");
- if ($sql) {
- $success .= '- Image updated successfully.<br />';
- } else {
- $fail .= "- Failed to update the image.<br />";
- }
- }
- }
- }
- header("location:../admincontrol?result_s=".urlencode($success)."&result_f=".urlencode($fail));
- exit();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement