uploading files and putting path into db

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
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

uploading files and putting path into db

Post by fariquzeli »

I am having some serious trouble with this upload script. I am trying to make it so that I can upload a .pdf newsletter and have it be uploaded to the server as well as put in a mysql database. Here is my code, beware:

Code: Select all

<?php
if (!IsSet($action)) {
?>
            <form action="<?=$PHP_SELF;?>" method="post" enctype="multipart/form-data" name="fileUpload">
              <table width="100%" border="0" cellspacing="3" cellpadding="0">
                <tr> 
                  <td width="25%"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Newsletter 
                    Title:</font></td>
                  <td width="75%"><input name="name" type="text" id="name"></td>
                </tr>
                <tr> 
                  <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">File 
                    Attachment (.pdf):</font></td>
                  <td><input type="file" name="pdfnewsletters"></td>
                </tr>
                <tr> 
                  <td>&nbsp;</td>
                  <td> <input type="hidden" name="username"value="<?php echo("$username"); ?>"> 
                    <input name="action" type="submit" id="action" value="Submit"> 
                    <input name="Reset" type="reset" id="Reset" value="Reset"></td>
                </tr>
              </table>
            </form>
            <?php 
}
else {
copy($_FILES['pdfnewsletters']['tmp_name'], "../downloads/".$_FILES['pdfnewsletters']['name']) or die("could not copy");
echo "";
echo "Name: ".$_FILES['pdfnewsletters']['name']."";
echo "Size: ".$_FILES['pdfnewsletters']['size']."";
echo "Type: ".$_FILES['pdfnewsletters']['type']."";
$path = "http://www.pancorp.com/newsletter/downloads/".$_FILES['pdfnewsletters']['name']";
mysql_connect($hostname, $user, $pass);
mysql_select_db("pancorp");
$sql = "INSERT INTO newsletters SET
name = '$name',
link = '$path',
username = '$username'";
$result = mysql_query($sql) or die(mysql_error());

}

?>
the upload.php file resides in the /newsletter/admin directory and the directory I want stuff uploaded to is /newsletter/downloads. I've never really written an upload script before so if you could help I'd appreciate it a bunch! Thanks

here are the errors I get:
Warning: Undefined variable: _FILES in /home/ttoomey/pancorp.com/newsletter/admin/upload.php on line 55

Warning: Undefined variable: _FILES in /home/ttoomey/pancorp.com/newsletter/admin/upload.php on line 55

Warning: Unable to open '' for reading: No such file or directory in /home/ttoomey/pancorp.com/newsletter/admin/upload.php on line 55
could not copy
kitsch
Forum Newbie
Posts: 12
Joined: Mon Nov 11, 2002 3:29 pm

Post by kitsch »

But what's the problem?
Or what error messages does the script give you?
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

sorry, i went in and edited my message after realizing the errors were missing.
kitsch
Forum Newbie
Posts: 12
Joined: Mon Nov 11, 2002 3:29 pm

Post by kitsch »

Just an ideea.
What version of PHP are you using? Because:
In PHP versions prior 4.1.0 this was named $HTTP_POST_FILES and it's not an autoglobal variable like $_FILES is.
Or have you used these kind of superglobals already?
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

i am using php 4.0.6 which would explain that, but I was having troubles using those variables anyways, of course I re-coded how I was doing it anyways, let me try it with those variables...

ahh only one error message now:
Warning: Unable to create '../downloads/1.pdf': Permission denied in /home/ttoomey/pancorp.com/newsletter/admin/upload.php on line 55
could not copy
i've pasted the line 55 here:

Code: Select all

<?php
copy($HTTP_POST_FILES['pdfnewsletters']['tmp_name'], "../downloads/".$HTTP_POST_FILES['pdfnewsletters']['name']) or die("could not copy");

?>
please help!
kitsch
Forum Newbie
Posts: 12
Joined: Mon Nov 11, 2002 3:29 pm

Post by kitsch »

Set the permissions for your download directory allowing the script to access it. Usually CHMOD-ing to 777 (octal) works.

You can find the CHMOD option in any better FTP client. If it pops up a box with checkboxes just check all boxes allowing all 3 categories to read, write and execute the files there.

This should solve your problem.
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

i tried chmodding it in putty and i get a permission denied, or directory doesn't exist everytime, its odd.
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

for some reason when I chmodded a directory with files in it it wouldn't work. All I had to do was create a different directory and chmod that one.


Thanks for the help!
Post Reply