Best way to fix errors?

I am working on a script for the photo gallery tutorial, the upload.php part, that is mostly just checking file types, getting the proper extension, and resizing them.

When try to run it, it just loads continuously, no error message… I searched around and saw that if a page is doing that, you probably have a problem with the loop. I checked, and I did see that I left out the increment operator. I added it in, and I’m still getting the same thing.

When I run it, the error I get in my console is that there’s an undefined variable. It’s a variable that gets initialized through the files actually being uploaded. I had the same problem with a get in another part of the project, and I fixed that with isset. It doesn’t work with $_FILES array.

My main question is what to do in this specific instance, and also what’s the best thing to do in the future if something isn’t working, but it’s not giving you errors.

Depends on what IDE or editor you are using.

I use Sublime Text Editor and put a breakpoint to inspect variables in the file then gradually move the breakpoint down, refresh the browser until I have established the problem,

<?php 
error_reporting(-1); ini_set('display_errors', true);

function fred($val='Nothing Passed?') {
echo '<pre>'; print_r($val); echo '</pre>'; die;
}

// fred($_POST);

if ($_POST['submit') {
  fred($_FILE);
  // 
}

Beware of sytax errors in above script, not tested.

For languages where there isn’t an interpreter around, I’ve always tried to debug by adding in plenty of echo() statements so you can see how far your code gets before it breaks, you can display values of stuff and narrow down exactly where the code is deviating from what you expect.

1 Like

The best way to debug is using xdebug. Being able to step through code line by line as it executes and evaluate expressions is an invaluable tool. When a local environment that is the ONLY way to debug. Remote environments can be tricky considering the level of flexibility a provider allows to add extensions like xdebug. However, locally there is absolutely no reason besides laziness to not do so. I don’t ever know how I lived without xdebug. Poor mans debugging is a time wasting mess for the birds. Get yourself set-up with xdebug and start debugging like a pro and saving a lot of time.

My files array isn’t getting filled. Is there anything in here that would mess it up on the preupload portion? I checked it against this stackoverflow post: http://stackoverflow.com/questions/3586919/why-would-files-be-empty-when-uploading-files-to-php

<form enctype='multipart/form-data'  action='upload.php' method='post'  name='upload_form'>
<tr><td><tr><td>Photo:  <input name=photofilename[] type=file></td></tr>
<tr></tr>Caption: <textarea name=photocaption[] cols=30 rows=2> </textarea>
<input type='submit' name='submit'  value='Add Photos'></table></form></td></tr>```

$images_dir =$_SERVER['DOCUMENT_ROOT'] .'/gallery/photos';

This is the directory on where to put the files in my XAMP folder, project starts in www

Thanks. I went ahead and a youtube video on it by JetBrains after reading your post.

You don’t need to have JetBrains software to use it. Eclipse and Netbeans which are free integrate with it. Though I do HIGHLY recommend PHPStorm but that is another topic.

Just to give more information, i went ahead and erased all of the code in upload.php, except for the piece that initializes a variable with the contents of $_FILE, and told it to echo if it was successful or not.

When I go to upload on preupload.php, where the forms are, it just keeps loading over and over. I’m confident it’s not a problem on the upload.php end, because earlier I could get over to that page, and it would just error out $file_list not initialzed(the container for the $_FILES array.)

The only thing I did different between now and earlier, is add in quotes o the HTML after the equal signs, because some were missing.

You might want to start a new topic for this one, and post your code.

narrow down exactly where the code is deviating from what you expect.

This is one of the most useful troubleshooting concepts in PHP that I’ve discovered - really, not just PHP - but the ability to follow your data and find the point where it stops being what it’s supposed to / doing what it’s supposed to / etc - then you know what part of your application is the culprit and can begin trying to diagnose why it’s broken.

This is why I liked the function I supplied earlier. Fred($variable); is quick to type, formatted output and stops execution. Just move it down through the script until the script shows the actual variable and not what is expected.

print_r() can be substituted for var_dump() if more detail is required.

Edit:
As @oddz mentioned I must try xdebug when I am back on the desktop. I have tried it before and cannot remember why I stopped using it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.