I'm just trying to make a simple image-upload feature in php, but for some reason it wont complete the task. It gets stuck on a white screen with the url being */action.php, and sometimes I get an error on that page, sometimes I get nothing (as of now).
I'm working through XAMPP if that matters.
(i'm aware of the lack of prepared statements)
action.php:
$target_directory = "/Applications/XAMPP/xamppfiles/htdocs/uploads/";
$filename = $_POST['fileLocation'];
$temp_filename = $_FILES["file"]["tmp_name"];
$size = $_FILES["file"]["size"];
// Produce a unique filename
$randomNumber = time();
$filetype = substr($filename, strrpos($filename, '.'));
$target_file = $target_directory . $randomNumber . $filetype;
$local_path = "/Applications/XAMPP/xamppfiles/htdocs/uploads/" . $randomNumber . $filetype;
header('Content-type: text/html; charset=utf-8');
if(!move_uploaded_file($temp_filename, $target_file))
header( 'localhost/index.php', 301 );
// = = = = Add to database = = = =
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("gps");
// Collect data from form
$imagetext = $_POST["imagetext"];
$locationstring = $_POST["locationstring"];
$query = "INSERT INTO gps (imagetext, file, locationstring) VALUES ('$imagetext', '$local_path','$locationstring')";
mysql_query($query);
mysql_close();
header( 'localhost/index.php', 301 );
Errors are switching between:
Warning: move_uploaded_file(/uploads/images/1495626347.psd): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/action.php on line 20
+
Warning: move_uploaded_file(): Unable to move '/Applications/XAMPP/xamppfiles/temp/phpYsJkjq' to '/uploads/images/1495626347.psd' in /Applications/XAMPP/xamppfiles/htdocs/action.php on line 20
and
Warning: File upload error - unable to create a temporary file in Unknown on line 0
However, the files are where they should be locally.
HTML (if someone would want to take a look):
<form method="POST" action="action.php" enctype="multipart/form-data">
<input id="fileLocation" name="fileLocation" type="text" class="form-control" placeholder="Image to upload" style="display:none">
<button type="button" value="Browse..." id="browseBtn" class="title">Select files...</button>
<input type="hidden" name="locationstring" style="display:none;"/>
<input type="file" name="file" id="hiddenFile" style="display: none;"/>
<input id="content" name="imagetext" class="form-control" type="text" placeholder="Text Below Image"/>
<input type="submit" value="Upload" name="submit">
</form>