Hi Guys,
I wondered if you might be able to help me. I am currently writing a script that will upload files to my server, I will then load a url to the image into my database for later use.
At the moment I can't figure out if this script is working or not. Because it all runs through, doesn't produce any errors. But then when I look at the directory it is supposed to upload to, there is nothing there.
So I spent a few hours looking for bugs & stripped out the database element to make sure it wasn't interfering. Then all of a sudden, out of no where, all of the files showed up in the correct directory about 30 minutes later. Which has confused me no end.
Here is the code, I wondered if somebody might have time to have a quick browse and spent any glaring errors?
<?php
session_start();
//Session variables
session_register ("name");
session_register ("file");
// Read POST request params into global vars
$file = $_POST['file'];
$name = $_POST['name'];
//Parameters for file checks
$allowed_ext = array('jpg', 'gif', 'bmp', 'png'); // These are the allowed extensions of the files that are uploaded
$max_size = 2097152; // 2000000 is the same as 2MB
//Error codes set empty
$extensioncheck = "";
$sizecheck = "";
$existcheck = "";
$globalerror = "0";
$successcode = "0";
// Check Entension
$extension = pathinfo($_FILES['file']['name']);
$extension = $extension[extension];
if(!in_array($extension, $allowed_ext)) {
$extensioncheck="1"; }
else {
$extensioncheck="0";}
//Check File Size
if(($_FILES["file"]["size"] < $max_size)){
$sizecheck="0";
}else{
$sizecheck="1";
}
// Check File Exists
if(!isset($_FILES['file'])) {
$existcheck = "1";
}else {
$existcheck = "0";}
// Where the file is going to be placed
$target_path_upload = "../images/";
$target_path_db = "images/";
$file_tmp = $_FILES["file"]["tmp_name"];
$file_name = $_FILES["file"]["name"];
$complete_path_upload = "$target_path_upload/$file_name";
$complete_path_db = "$target_path_db/$file_name";
// If error codes have been thrown up, refer user back to register page
if ($existcheck=="1" || $extensioncheck=="1" || $sizecheck=="1") {
echo "$sizecheck and $existcheck and $extensioncheck and $globalerror and $successcode - fail";
session_destroy();
}
//If all error codes are clear, upload picture
elseif ($sizecheck=='0' && $existcheck=='0' && $extensioncheck=='0') {
$successcode="1";
move_uploaded_file($file_tmp, "$target_path_upload/$file_name");
echo "upload complete";
session_destroy();
}
//If unexpected error occurs, globalerror code is set and user referred back upload form
else {
$globalerror="1";
echo "$sizecheck and $existcheck and $sizecheck and $globalerror and $successcode - global";
session_destroy();
}
As always, thanks for all the help people