Help with php

I have a splash page that has banners on it and I want my members to be able to upload theyre banner to the page and placed at the bottom of the banners in the list.
Here is an example of what I have so far:

This is in my index.php file.

[code]
Filename:


[/code]

This is in my upload_file.php file.

[php]<?php

$allowedExts = array(“gif”, “jpeg”, “jpg”, “png”);
$extension = end(explode(".", $_FILES[“file”][“name”]));
if ((($_FILES[“file”][“type”] == “image/gif”)
|| ($_FILES[“file”][“type”] == “image/jpeg”)
|| ($_FILES[“file”][“type”] == “image/jpg”)
|| ($_FILES[“file”][“type”] == “image/pjpeg”)
|| ($_FILES[“file”][“type”] == “image/x-png”)
|| ($_FILES[“file”][“type”] == “image/png”))
&& ($_FILES[“file”][“size”] < 200000)
&& in_array($extension, $allowedExts))
{
if ($_FILES[“file”][“error”] > 0)
{
echo "Return Code: " . $_FILES[“file”][“error”] . “
”;
}
else
{
echo "Upload: " . $_FILES[“file”][“name”] . “
”;
echo "Type: " . $_FILES[“file”][“type”] . “
”;
echo “Size: " . ($_FILES[“file”][“size”] / 200000) . " kB
”;
echo "Temp file: " . $_FILES[“file”][“tmp_name”] . “
”;

if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
}

}
else
{
echo “Invalid file”;
}

?> [/php]

I put it together and tried to upload a banner. I clicked on choose file and it opened a window to choose a file. Then I clicked submit and the page said that the banner was uploaded but I did not see it in the upload_file folder. And I cant figure out how to get it to add the banner to the page!
Any help would be GRRRREATLY APPRECIATED!
Thank You for your time,
Daniel M. :-[

The upload works fine.

  1. Do you use a proper IDE? (check out my signature)
  2. Do you have error reporting on?

Add this to the beginning of the PHP script and see if you get any errors (I know you will ^^)

[php]<?php
ini_set(‘error_reporting’, E_ALL);
ini_set(‘display_errors’, ‘1’);[/php]

You will get a “Strict standards: Only variables should be passed by reference in /srv/www/test/public/index.php on line 8”, you can ignore it if you want, but I find it’s better to clean up all warnings and errors while you can. It refers to this:

[php]$extension = end(explode(".", $_FILES[“file”][“name”]));[/php]

“end” doesn’t like beeing fed a result of a function, just split it up and you’ll be fine.

[php]$fileNameArray = explode(".", $_FILES[“file”][“name”]);
$extension = end($fileNameArray);[/php]

This isn’t the upload problem though, I’m guessing it’s because (lack of) permissions. If you get something like this:

Warning: move_uploaded_file(upload/file.jpg): failed to open stream: Permission denied in /srv/www/test/public/index.php on line 22

Then you need to give the web server write permission to the upload folder.

[hr]

In order to show the banners on the website you should insert the banner into a database. And then fetch banners from here when you want to display them.

Something like this:

User uploads banner, banner is stored in upload/filename.extension, banner name is inserted into database

In the script(s) where you want to show it
‘SELECT filename FROM banner’

then print them however you want.

I would of course suggest adding them to the database with something like active = 0, and then manually accepting banners. If not you will get some trouble with unwanted stuff. You should also add a captcha for all non-verified users.

Sponsor our Newsletter | Privacy Policy | Terms of Service