1

I have been working on a project for quite some time and one of the main functions of the system i am developing is the file upload, i had it setup so that the image was being stored in a directory with the file name storing in the database/ This was all working fine but i was running through things today and my file upload is suddenly no longer working and i have no idea why?

If anyone can shed some light on what is wrong for me it would really be appreciated!

File upload function (based on example at W3Schools)

function upload_file(){
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["page_main_image"]["name"]);
$extension = end($temp);
if ((($_FILES["page_main_image"]["type"] == "image/gif")
        || ($_FILES["page_main_image"]["type"] == "image/jpeg")
        || ($_FILES["page_main_image"]["type"] == "image/jpg")
        || ($_FILES["page_main_image"]["type"] == "image/pjpeg")
        || ($_FILES["page_main_image"]["type"] == "image/x-png")
        || ($_FILES["page_main_image"]["type"] == "image/png"))
    && ($_FILES["page_main_image"]["size"] < 200000)
    && in_array($extension, $allowedExts))
{
    if ($_FILES["page_main_image"]["error"] > 0) {
        echo "Return Code: " . $_FILES["page_main_image"]["error"] . "<br />";;
    }
    else {
        echo "Upload: " . $_FILES["page_main_image"]["name"] . "<br />";
        echo "Type: " . $_FILES["page_main_image"]["type"] . "<br />";
        echo "Size: " . ($_FILES["page_main_image"]["size"] / 1024) . " kb<br />";

        if (file_exists("uploads/" . $_FILES["page_main_image"]["name"]))
        {
            echo $_FILES["page_main_image"]["name"] . " already exists. ";
        }
        else
        {
            move_uploaded_file($_FILES["page_main_image"]["tmp_name"],
                "uploads/" . $_FILES["page_main_image"]["name"]);
            echo "Stored in: " . "uploads/" . $_FILES["page_main_image"]["name"] . "<br />";
            $image="{$_FILES['page_main_image']['name']}";
        }
    }
}
else {
    echo "Invalid file";
}

return $image;
}

The form processing is as follows:

<?php
if (isset($_POST['submit'])) {
//Process the form
$image = upload_file();

$project_id = $_POST['project_id'];
//var_dump ($project_id);

$wireframe_title = mysql_prep($_POST["wireframe_title"]);
$browser_title = $_POST["browser_title"];
$url_key = $_POST["url_key"];
$wireframe_type = $_POST["wireframe_type"];
//$image = $_POST["page_main_image"];
$page_bg_color = $_POST ["page_bg_color"];

$query  = "INSERT INTO wireframes (";
$query .= " project_id, wireframe_title, browser_title, url_key, wireframe_type, page_main_image, page_bg_color";
$query .= " ) VALUES (";
$query .= " '{$project_id}','{$wireframe_title}', '{$browser_title}', '{$url_key}', '{$wireframe_type}', '{$image}', '{$page_bg_color}' ";
$query .= ")";

echo $query;

try { $result = mysqli_query($connection, $query);
} catch (Exception $e) {
    return 'Caught exception: '+  $e->getMessage()+ "\n";
}
//Test if there was a query error
if ($result) {
    //Success
    // would normally use a redirect ie redirect_to("somepage.php");
    //$message = "Subject created.";
    redirect_to("wireframes.php?id=$project_id");
}else {
    //failure
    //$message = "Subject creation failed.";
    //redirect_to("add_project.php");
    echo $query;
}
} else {
// This is probably a GET request
redirect_to("add_edit_wireframe.php?id= echo $_GET[$project_id]");
}
?>

I literally have no idea why it has stopped all of a sudden i only used the image upload on Saturday and it worked fine and haven't touched it since.

EDIT: OK as an update for some reason it is allowing me to upload certain images but not others. could it possibly be due to the file size? I have changed the max_upload size to 4mb in the php.ini file but this hasn't helped. Is there a way to test for this?

I notice in the function i am using for handling the file upload at the start it sets size at < 200000 and then further down it has size / 1024 kb

can someone please explain this to me so i understand what is going on exactly, if i knew this maybe i can change the numbers to allow for the upload of larger files?

DannyW86
  • 201
  • 3
  • 8
  • 20
  • Two things that have caught me out in the past are the `enctype="multipart/form-data"` on the form tag and the file path to the upload location being invalid. Are both correctly implemented? – ScottMcGready May 20 '14 at 00:04
  • yes i have set the enctype to multipart/form-data and it posts to a page containing the above form processing. Like i said in the original post this was working it has just suddenly decided not to and i have no idea what i have done for this to happen! – DannyW86 May 20 '14 at 00:10
  • `var_dump($_POST)` at the top of your script. Make sure everything is coming through that should be – ScottMcGready May 20 '14 at 00:13
  • Is there anything appearing in your server error log? –  May 20 '14 at 00:16
  • [Turn on all errors](http://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings#answer-5438125) and show us any errors that occur. Also, test the form and show us `var_dump($_POST);` and `var_dump($_FILES);`. You do not check everything so errors can easily occur without you knowing. – Sverri M. Olsen May 20 '14 at 00:18
  • Sorry how exactly do i do this? All errors have already been turned n in the php.ini file but where exactly should i be looking for these errors? No errors are displaying on the screen as the form is still processing, all the rest of the form data is being sent to the database bar the filename – DannyW86 May 20 '14 at 00:23
  • Wouldn't hurt to ask - Is `uploads` folder writable? – asprin May 20 '14 at 10:19
  • Yes, definitely, as i said some images are saving just not all! I have to think it is something to do with file upload size now! – DannyW86 May 20 '14 at 10:32

0 Answers0