0

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>
Martin
  • 20,858
  • 7
  • 60
  • 113
Joel
  • 4,537
  • 2
  • 33
  • 53
  • could be a permissions thing - based pm your file path - you using OS X? – treyBake May 24 '17 at 13:21
  • Have to ask -- are you out of space on your filesystem? – Tim S. May 24 '17 at 13:21
  • @ThisGuyHasTwoThumbs Correct, i'm on OSX. I've tried chaning permissions to chmod -R 777 but that led to me having to reinstall xampp due to permission violations. – Joel May 24 '17 at 13:22
  • In the question you describe errors saving the file, but the question title says the file is saved but you have a database error. Which is it? – David May 24 '17 at 13:22
  • @TimS. if that was the case i'd be glad, but unfortunately no. 30gb available. – Joel May 24 '17 at 13:22
  • @David I'm just as confused as you are. – Joel May 24 '17 at 13:23
  • @Joel yeah don't 777 ;) chmod is a good starting point but chown might be more applicable, Apache user needs to be in the same group as your user account, create a user group called www and add Apache and yourself to it and try running the script after that – treyBake May 24 '17 at 13:23
  • Check this one. [Reference](https://stackoverflow.com/questions/17153624/using-php-to-upload-file-and-add-the-path-to-mysql-database) – Virb May 24 '17 at 13:23
  • `header( 'localhost/index.php', 301 );` that line should also contain `http://`. It won't fix your code but it's required and adding `exit;` after each header. – Funk Forty Niner May 24 '17 at 13:24
  • ***Stop using MySQL***, it's DEPRECATED, old and insecure. And has been for **at least 5 years** (!) Instead upgrade and update to `Mysqli_` or `PDO` functions. – Martin May 24 '17 at 13:24
  • @Joel: Perhaps you can clarify then? Your code is doing two things: (1) Saving an uploaded file to the file system and (2) adding a record to a database. You've alluded to problems with both, but also alluded to both working. So what is the *actual specific problem* you're asking about? – David May 24 '17 at 13:25
  • @Fred-ii- Woups x) didn't solve anything tho as i believe the problem lies a lot deeper. – Joel May 24 '17 at 13:27
  • 1
    @Joel which is why I wrote: *"It won't fix your code"* ;-) – Funk Forty Niner May 24 '17 at 13:27
  • @Joel if it's not adding to the db, then use `mysql_error()` on the query. I take it that you've connected successfully though with the mysql_ api? and check the columns' lengths. Could be a duplicate issue here in db or something else. – Funk Forty Niner May 24 '17 at 13:30
  • @David Alright, i see what you mean now. The files are stored at the specific path they are supposed to. *Before* with no change the files didn't upload and gave an error on the function `move_uploaded_files()`. Now instead, i'm getting the 2nd error or just a blank page. Hope that clarifies it a bit. – Joel May 24 '17 at 13:30
  • @Fred-ii- Just to be sure, i've set the lengths to `varchar(300)` didn't solve it either. – Joel May 24 '17 at 13:31
  • @Joel: `"Just to be sure, i've set the lengths to varchar(300)"` - To be sure of what? If the data isn't being added to the database then, as advised, you need to check for errors with `mysql_error()`. In the question you also indicate that you're aware of the SQL injection problem, so why not fix it? – David May 24 '17 at 13:35
  • @David [Just as I told the guy here...](https://stackoverflow.com/questions/44159588/image-is-uploaded-but-nothing-is-added-to-database?noredirect=1#comment75336038_44159588) ;-) – Funk Forty Niner May 24 '17 at 13:37
  • and if these `header( 'localhost/index.php', 301 );` were changed to `header( 'http://localhost/index.php', 301 ); exit;` then the question should be updated and making sure the tmp folder also has the permissions to write to and isn't full. – Funk Forty Niner May 24 '17 at 13:38
  • I've tried to run the code on a windows machine with mysqli variables instead (php 7.0) same thing – Joel May 24 '17 at 13:45
  • doing exit; after the header didn't do anything. still stuck on that white screen. – Joel May 24 '17 at 13:46
  • 1
    Joel, @Fred-ii- , the header needs to include `"location:` **:** `header( 'Location: http://localhost/index.php', 301 ); exit;` – Martin May 24 '17 at 14:40

0 Answers0