# Techies: Help a Flareon!



## BRN (Jan 16, 2013)

Hi, gang.

Okay, let me throw this out at a high-level. A friend of mine is a computer salesman, who hosts websites and servers. Unfortunately, he's having some trouble creating a picture uploader.

His goal is to allow the user to select a file to upload, hit upload, and upload it.

This is the man's work so far, but as you might note, attempting to use it yields this page:

```
// Where the file is going to be placed 
$target_path = "darkmirage/"; 
/* Add the original filename to our target path. Result is "darkmirage/filename.extension" */ $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
 if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
{
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
} 
else
{ 
echo "There was an error uploading the file, please try again!"; }]
```

Given this information, can anybody here provide help or advice, or even a useful link or tutorial?


----------



## Kazooie (Jan 16, 2013)

Link to the full set of code is broken :x

Maybe just pastebin it somewhere?


----------



## Sai_Wolf (Jan 16, 2013)

SIX said:


> Hi, gang.
> 
> Okay, let me throw this out at a high-level. A friend of mine is a computer salesman, who hosts websites and servers. Unfortunately, he's having some trouble creating a picture uploader.
> 
> ...



At a glance, that looks like PHP (feel free to correct me if I'm wrong). And the link's broken, so I'm going to hazard a guess that when you click said link; you get code on display.

I did notice that the extension was '.html', and unless you specifically told the webserver to execute PHP for *.html (Apache's AddHandler, etc), then it's just going to treat it like HTML (i.e. spit the code out instead of executing it). He either needs to rename it to .php or check his webserver settings.

As for upload tutorials? They're a dime a dozen. It's a relatively simple task.


----------



## BRN (Jan 16, 2013)

Aha... alright, fairly played. The change to .php seems to have literally fixed his issue of allowing things to upload. You beautiful thing, Sai.


The next task he's facing is about redirecting the user to a list of the uploaded files once the upload completes. I assume that would involve a .ftp extension, or am I being foolish here?


----------



## Sai_Wolf (Jan 16, 2013)

SIX said:


> Aha... alright, fairly played. The change to .php seems to have literally fixed his issue of allowing things to upload. You beautiful thing, Sai.
> 
> 
> The next task he's facing is about redirecting the user to a list of the uploaded files once the upload completes. I assume that would involve a .ftp extension, or am I being foolish here?



You're not being foolish, just a bit off is all. The extension .php or .html merely tells the web server HOW to display the file. (.php <-- This file has PHP code to execute. | .html <-- This file has pure markup. No need for code execution.) The accepted practice is just to embed HTML in .php files as needed. PHP is perfectly fine with this. (As is Ruby, ASP.NET, etc.)

Your question is a bit easier to answer with the above in mind. All he'd have to do is, after the successful message, to either display a link (Easiest) or to auto redirect (Bit harder) them to another page.



```
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
/* Display a link */
echo "<a href="displaypics.php">Click here</a> for the list of pics.";

/* -OR- automatically redirect them. You'd have to enable headers modification to use this, as by default, PHP doesn't allow the headers
to be changed after the page loads */

header(location: http://yoursite.com/displaypics.php);
}
```

It should be noted that the header example isn't the ideal way to go. Headers have to be sent before any echo occurs on the page (You can force otherwise, but you shouldn't in the first place.) In this scenario, it might be best to just go with a link.

It's been ages since I fooled with PHP, but I hope that at least helps. Also This thread has some solutions, if a redirect is the way your friend wants to go.


----------



## BRN (Jan 16, 2013)

This was genuinely helpful, and he's managed to get it working. I owe you a beer and my thanks. :3


----------



## Miles-za (Jan 27, 2013)

To do a redirection, it is usually good practice to have an automatic redirection as well as a link to manually redirect. FAF does that with posting for example.

Putting a link should be straightforward, but what about automatic redirection?

You basically have 3 ways to do that. First is to add this snippet of code to the head of the html page:


> <meta http-equiv="refresh" content="5;URL='http://example.com/'">


This is an html meta tag that redirects the user to another page after 5 seconds. Change the delay or the location to what you need.

The second method is to use a PHP header to automatically redirect to the page. This is done by adding this snippet of code:


> <?php header("Location: http://www.example.com/"); ?>



However this does not allow for a delay like the meta tag. There is also Refresh that does allow for a delay, just like the other:


> <?php header('Refresh: 5; url=http://www.example.com/'); ?>



Finally, you can do a Javascript redirection. It consists of simply adding this snippet of code in your Javascript code that will redirect the user to a new page after set delay:



> setTimeout(window.location.replace("http://www.example.com/"),5000);



This time the delay is in milliseconds, so be careful.

Anyway, the rest is up to him to see which method (or combination of) works best for him.


----------



## Toshabi (Jan 28, 2013)

I believe the problem has already been resolved Miles, but your contribution to the solution is still greatly appreciated! ^_^





@SIX: Tell Fyre that Toshabi still hates him and hopes that he gets shaved. >:[


----------



## BRN (Jan 28, 2013)

Toshabi said:


> @SIX: Tell Fyre that Toshabi still hates him and hopes that he gets shaved. >:[




I'll tell him at Otacon.

8)


----------

