file upload

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sinus
Forum Newbie
Posts: 10
Joined: Fri Nov 22, 2002 9:46 pm

file upload

Post by sinus »

Hi
I'm trying to allowed the upload of zip files through a web interface,, I have read many of the tutorials etc out there but still can't seem to get this to work. I wish to enter the file name into a database rather than the whole file. But all that gets entered into the database is other information that is sent at the same as the file is supposed to be uploaded.
The destination directory has been chmod to 777.
Here is the code

Code: Select all

<?php

// directory path to load file to
$uploaddir="/usr/local/apache/htdocs/archangels/maps/";

// to see if there is a file
if (($the_file == "none") || ($the_file == "")) &#123;
print ("You must specify a file to upload. ");
&#125; else &#123;

// check file type
if ($the_file_type != "application/x-zip") &#123;
print ("The file must be in zip format");
$zip = "no";
&#125; else &#123;
$zip ="yes";
&#125;

// check file size
if($max_file_size > 2000000) &#123;
print ("The file is to large");
$size = "large";
&#125; else &#123; 
$size = "small";
&#125;

//directory to upload to
if (($zip == "yes") && ($size == "small"))&#123;
copy($the_file, $uploaddir.$the_file_name);
unlink($the_file);
print ("file upload successfull");
&#125; else &#123;
print ("failed upload");
&#125;
&#125;
?>
ok i'm not to sure about the path to the target upload directory, but if everything else is fine i'll know what the problem is.
When I run the script i keep getting the error message with the first if conditional.

Any help is gratefully appreciated
Sinus
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Post by puckeye »

Hi sinus,

I don't see anywhere the insertion into the database...

Is your <FORM> enctype set to enctype=\"multipart/form-data\"? Also I don't know if it makes a difference but I usually use the POST method when uploading files...

Also you should use either move_uploaded_file http://www.php.net/manual/en/function.m ... d-file.php or at least is_uploaded_file http://www.php.net/manual/en/function.i ... d-file.php just to make sure that the file was properly uploaded...
sinus
Forum Newbie
Posts: 10
Joined: Fri Nov 22, 2002 9:46 pm

Post by sinus »

Hi puckeye

This is the form i am using

Code: Select all

<form method=post enctype="multipart/form-data" action=updatemaps.php?uname=$uname>
  <td width=230 align=left class=info>id number:</td><td width=400 class=info align=left><input type=text size=10 name=id></td></tr>
 <tr>
  <td width=230 class=info align=left>Map name:</td><td width=400 class=info align=left><input type=text size=30 name=mapname></td></tr>
  <tr>
  <td width=230 class=info align=left>Game type:</td><td width=400 class=info align=left><input type=text size=30 name=gametype></td></tr>
  <tr>
  <td width=230 class=info align=left>file:</td><td width=400 class=info align=left>
  <input type=hidden name="max_file_size" value="2000000">
  <input type=file size=30 name="the_file"></td></tr>
  <tr>
  <td width=230 class=info align=left valign=top>Description:<td width=400 class=info align=left><textarea cols=30 name=description rows=8></textarea></td></tr>
    <tr>
  <td width=630 align=left height=10 colspan=2 class=info></td>
  <tr>
  <td width=630 align=left colspan=2 class=info>
  <input type=submit name=add value="Add Map"> &nbsp;&nbsp;
  <input type=submit name=delete value="Delete Map">
  </td></tr>
  </form>
Then based on wether add or delete was used this code is implemented i'm just showing add for now

Code: Select all

if ($add) &#123;
require("functions/addupload.php");
$query1 = "INSERT INTO maps (name, gametype, description, url) VALUES ('$mapname', '$gametype', '$description', '$the_file')";
if (!($result1 = mysql_db_query ($databasename, $query1, $connection)))&#123;
print ("Error no map added");
&#125;else &#123;
print ("Map added");
&#125;
&#125;
The require function here calls in the first piece of code i put up to execute the upload. The sql statment executes because i get map added response. By the way the page is pasting to it's self.

Thanks for the help, I'll look into move_uploaded_file & is_uploaded_file

Sinus
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Post by puckeye »

In

Code: Select all

$query1 = "INSERT INTO maps (name, gametype, description, url) VALUES ('$mapname', '$gametype', '$description', '$the_file')";
You use the variable $the_file. If you didn't modify this variable before the query string it should return the file's location something like /tmp/sdagfklhagf

You should use $the_file_name like in your verification script.

Also why did you write action=updatemaps.php?uname=$uname?

You should use a hidden field

Code: Select all

<INPUT TYPE="hidden" NAME="uname" VALUE="$uname">
it does the same thing...
DeGauss
Forum Contributor
Posts: 105
Joined: Tue Oct 22, 2002 9:44 am
Location: Gainesville, FL

Post by DeGauss »

Here's an upload class which has never failed to work for me... Since i wrote it ;)

http://www.vanillacity.com/geek/items/suck.phps
sinus
Forum Newbie
Posts: 10
Joined: Fri Nov 22, 2002 9:46 pm

Post by sinus »

Ok having looked at your recommendations on using is_uploaded_file and move_uploaded_file I have tried the floowing code. Though it still does not get passed the first conditional

Code: Select all

$uploaddir="/usr/local/apache/htdocs/elysuimdesign/archangels/maps/";
$allowedfile="application/x-zip";

// check that file is seleted
 if (!is_uploaded_file($the_file)) &#123;
 print ("No file selected");
 &#125;
 else &#123;
 
//check the file type
if ($_FILES&#1111;'the_file']&#1111;'type'] != $allowedfile) &#123;
print ("The file type is not allowed");
&#125;
else &#123;

//check the file size
if ($_FILES&#1111;'the_file']&#1111;'size'] > $max_file_size) &#123;
print ("The file was to large");
&#125; 
else &#123;

// move file to selected directory
if (move_uploaded_file($_FILES&#1111;'the_file']&#1111;'tmp_name'], $uploaddir . $_FILES&#1111;'the_file']&#1111;'name'])) &#123;
 print "File is valid, and was successfully uploaded";
 &#125; else &#123;
  print "File not uplloaded";
  &#125;
  &#125;
  &#125;
  &#125;
Thanks for the help with writing to the database that is ok now

Sinus
Post Reply