Jump to content

File uploads


DjMikeWatt

Recommended Posts

Hi everyone...

 

I've recently just started working with php file upload scripts... I have a simple one working, but now I need to do something a little more complicated and I'm hoping to get some help... Example code is very useful to me - I learn best by seeing it, and then sort of reverse engineering the code.

 

Here's the situation:

 

An upload form that allows the user to choose a year from a menu, a month from another menu, enter a short description in a text field, then choose a file from the file upload field.

 

Now, the point is that the script uploads the file to a directory based on the year and month chosen in the field (i.e. "/audio/2009/August/".  I also need the description and file name (as uploaded) entered into a MySQL DB (so that the file can be located later using a DB query.)

 

So, is this going to be as complicated as it seems?  Help is appreciated.

 

+mf

Link to comment
Share on other sites

no...just requires some thinking. i just wrote an image upload script. Although i wouldn't have a bunch of different directories. I would just rename the file to something that i could use to pull back up. For example, i have an event driven site. People create and post events and i give them the option to upload an image. Well, i take that image and rename it. I rename it by getting the timestamp of the end datetime+the username+then the event id#. So an example would be "1249441200LAgile35612.png". then after i rename it i put that name in the database along with the rest of the event info. Then i run a cron job to pull that number off the front of the name and check to see if that number is less than today's timestamp...if it is--> delete it. That way they don't keep piling up and take up space. So if i were you, i would use a method similar to mine...i don't have to go back in and figure out what needs to be deleted..it gets done automatically

Link to comment
Share on other sites

Thanks for the reply.

 

In this case, the files would never be deleted - they'll be left on the server indefinitely as part of an archive.  And as far as the file name, it's not necessary for me to rename it.  The reason being that the file is uploaded, and the file's name is inserted into a column in the DB... the php script that displays the file on the page uses that DB to recall the file.

 

So, for example, these are all MP3s I'm talking about.  I have a column called "piece_1" and that value may be "sample_1.mp3" or "wehfweoih.mp3."  When the page goes to display that file, it's displaying $row_selected['piece_1'], so the file's name is irrelevant.

 

I think that all makes sense, anyway.  :-)

Link to comment
Share on other sites

gotcha, then do something like this.

 

<?php
$source = $_FILES['fupload']['tmp_name'];
$filename = $_FILES['fupload']['name'];
$path_to_mp3_directory = $_POST['year']."-".$_POST['month']."/";
$target = $path_to_mp3_directory . $filename;
move_uploaded_file($source, $target);

//then code to put that $filename into the db//

?>

 

ok, now say the user inputted february of 2010 with their file. The file would then get saved to that directory via "2010-02/whatever.mp3". Thats assuming you have a directory "2010-02" though...not sure how you named them.

Link to comment
Share on other sites

You see, the code on my upload script looks like this (and works just fine):

<?php 
$target = "audio/".$_POST['year']."/".$_POST['month']."/"; 
$target = $target . basename( $_FILES['piece_1']['name']) ; 
$ok=1; 
if(move_uploaded_file($_FILES['piece_1']['tmp_name'], $target)) 
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} 
else {
echo "Sorry, there was a problem uploading your file.";
}
?>

 

The problem is that I don't know how to add an INSERT behavior to the submitting page, because when I do, the upload no longer functions.  It inserts the record, but the file no longer makes it to its destination... seems to be a "one or the other" situation - I know there's a way to do this.  I just can't get it.

Link to comment
Share on other sites

Okay, so I'm ALMOST there... could really use a hand with this last bit, though.

 

I've gotten it to insert the record and upload the file.  I'm using a hidden form on the upload script that inserts the record based on the fields submitted on the upload hmtl form and submitting that form when the upload script loads using js.

<body onLoad="document.insert.submit()">

 <?php 
$target = "audio/".$_POST['year']."/".$_POST['month']."/"; 
$target = $target . basename( $_FILES['piece_1']['name']) ; 
$ok=1; 
if(move_uploaded_file($_FILES['piece_1']['tmp_name'], $target)) 
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} 
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
</p>
<form id="insert" name="insert" method="POST" action="<?php echo $editFormAction; ?>">
  <input name="account_id" type="hidden" id="account_id" value="<?php echo $_POST['account_id'] ; ?>" />
  <input name="year" type="hidden" id="year" value="<?php echo $_POST['year'] ; ?>" />
  <input name="month" type="hidden" id="month" value="<?php echo $_POST['month'] ; ?>" />
  <input name="txt_piece_1" type="hidden" id="txt_piece_1" value="<?php echo $_POST['txt_piece_1'] ; ?>" />
  <input name="piece_1" type="hidden" id="piece_1" value="<?php echo $_POST['piece_1'] ; ?>" />
<br />
<input type="hidden" name="MM_insert" value="form1" />
</form>

 

My ONLY remaining problem is that one of the fields in db is supposed to be the name of the file.  If the user uploads "sample1.mp3", then I want the field "piece_1" to have the varchar value of "sample1.mp3".  It appears that file fields do not pass this info along in the standard way (I can't just call it like I would a normal form field - returns null).

 

Can I use php to call that value? Like maybe something like:

<?php echo $_FILES['piece_1']['name']; ?>

 

(I'm sorry if this is obvious - Like I'm answering my own questions... It helps me a lot to work things out on boards like this - and, my hope is, that "showing my work" will  help others who visit the board later looking for answers like this!)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.