Here is my error:
(Added a comment to line 55) One idea would be to modify the script to not have to move the file, but no idea how to do that. Not sure if that would even work because I need it to be outputted to the uploads folder with the same name the file had initially.
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Title:<br>
<input type="text" name="imgTitle" required><br><br>
Upload a File: <input type="file" name="imgUploaded" id="imgUploaded"><br><br>
Comments: <br>
<textarea name="imgDesc" rows="4" cols="35"required></textarea><br><br>
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
<?php
if (isset($_POST["submit"]) ) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["imgUploaded"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$imgTitle = $_POST['imgTitle'];
$imgDesc = $_POST['imgDesc'];
$servername = "sn";
$username = "un";
$password = "pw";
$dbname = "db";
echo $imgDesc;
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["imgUploaded"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["imgUploaded"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["imgUploaded"]["tmp_name"],$target_file)) // this is line 55
{
$imgPath = $_FILES["imgUploaded"]["name"];
$conn = mysqli_connect($servername, $username, $password, $dbname);
$sql = "INSERT INTO BrianJones (sqlName, sqlDesc, sqlPath)
VALUES ('$imgTitle', '$imgDesc', '$imgPath')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
echo "The file ". basename( $_FILES["imgUploaded"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
Not sure how to fix this, obviously it's an error when trying to move my temp image to the uploads folder because I don't have permission. How can I get around this?
Note: I cannot give myself any permissions, it works fine on localhost but I have to upload it to a web server and test, I can only modify the php and not change any server settings.