Hi I've got this file upload for a site, it puts the files in to a directory on the site called /f/sf (thats the path from the route folder) well it should do that at least but for some reason it doesn't upload them and I cant figure it out!
Also I know there's a line which connects to a database but that works correctly I know because the details of the file are being posted in to the database, so i figured the PHP section is the best place to come!
Here's the code
PHP Code:
<?php
//Checks if there is a login cookie
if(isset($_COOKIE['******']));
else{header( 'Location: http://www.****.php?error=Sorry+please+login' );
}
//This is the directory where images will be saved
$folder = "f/sf/";
//This gets all the other information from the form
$name=$_COOKIE['*****'];
$pic=($_FILES['photo']['name']);
$ip = getenv('REMOTE_ADDR');
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
//This applies the function to our file
$ext = findexts ($_FILES['photo']['name']) ;
//This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
$ran = rand () ;
//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 = $ran.".";
//This combines the directory, the random file name, and the extension
$target = $ran2.$ext;
// Connects to your Database
mysql_connect("***") or die(mysql_error()) ;
mysql_select_db("****") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `file_sf` (name, ip, photo, target) VALUES ('$name', '$ip', '$pic', '$target')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $folder))
{
//Tells you if its all ok
header( 'Location: http://www.****.php?read=File+uploaded+successfully' );
}
else {
//Gives and error if its not
header( 'Location: http://www.******.php?read=Sorry,+there+was+a+problem+uploading+the+file.+Please+try+again');
}
?>
Just to clarify that code should do all the following
- rename the file with a random number to prevent files with the same name being over written
- then post this random name (with the file extension on the end) to the database < this works currently
and upload the file to the directory