Current location: Hot Scripts Forums » Programming Languages » PHP » Getting and passing the file name during upload


Getting and passing the file name during upload

Reply
  #1 (permalink)  
Old 03-26-04, 05:06 PM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
Getting and passing the file name during upload

I have been working on a "script" all day and it has been one step forward and two steps back !!!

I am off to bed now as had enough.

Could someone help me with the problem below ready for the morning : )

Anthony

I am up loading some files using :

PHP Code:

  $numoffile 5;

  
  
$file_dir  "/upload/up/";
  
  if (
$_POST) {
    for (
$i=0;$i<$numoffile;$i++) {
      if (
trim($_FILES['myfiles']['name'][$i])!="") {
        
$newfile $file_dir.$_FILES['myfiles']['name'][$i];
        
move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile);
        
$j++;
      }
    }
  }
  if (isset(
$j)&&$j>0) print "Your file(s) has been uploaded.<br>";
  print 
"<form method='post' enctype='multipart/form-data'>";
  for(
$i=0;$i<$numoffile;$i++) {
    print 
"<input type='file' name='myfiles[]' size='30'><br>";
  }
  print 
"<input type='submit' name='action' value='Upload'>";
  print 
"</form>"
This works great and my files are up loaded BUT I need to get the filename and send it to Mysql and I have no idea how to do it.

I was getting the file names from the directory afterwards but I have added "unlink($filename);" now and the file disapears before I can get the name. The thumb directory has all the old and new files so they are repeated in the database - see what I mean about one step forward and 2 steps back - I blame NeverMind for this problem LOL
Reply With Quote
  #2 (permalink)  
Old 03-26-04, 10:44 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

I'm not sure if I'm following you quite right, but what about logging your $filename in an array or something before you unlink it (or them)? The logic here is, to unlink a file, you have to know the filename, and if you know the filename before unlinking it, log it for the later use.

Then you can just INSERT them into the database.

Excuse me if I'm off. =)
__________________
Blavv =|
Reply With Quote
  #3 (permalink)  
Old 03-27-04, 03:13 AM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
What I want

Thanks for looking blaw.

The script I am using allows me to "browse" my hard drive for the files I want to upload. I then click on "Submit" and they are uploaded to my host.

So I need to include in the bit of script someway of passing the name of the files to another page to then upload into Mysql.

So you are right in saying I need to log them in an array but I am not sure how to do it.

Anthony
Reply With Quote
  #4 (permalink)  
Old 03-27-04, 03:42 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

It's getting a little late where I am so I can't "stay" for a long time, but as I can see, you're letting the user browse the hard drive via input tag of type "file". then multiple files that are selected will be uploaded to the server where your script is running. Now, you need to simultaneously (well, not literally) INSERT the file name of the file you are uploading into MySQL (NOT the file itself, I mean). Is it correct?

I don't see any unlink() in your code, so again, I'm sorry if I'm off, but if you want to do what I just mentioned, simply bug an INSERT statement in a loop like so (One line added, accompanied with a comment):

PHP Code:

<?php

$numoffile 
5;

$file_dir    "/upload/up/";

if (
$_POST) {
    for (
$i=0;$i<$numoffile;$i++) {
        if (
trim($_FILES['myfiles']['name'][$i])!="") {
            
$newfile $file_dir.$_FILES['myfiles']['name'][$i];
            
move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile);
            
// Now INSERT.
            
mysql_query("INSERT INTO my_table (my_filename_field) VALUES ('".$_FILES['myfiles']['name'][$i]."')");
            
$j++;
        }
    }
}
if (isset(
$j)&&$j>0) print "Your file(s) has been uploaded.<br>";
print 
"<form method='post' enctype='multipart/form-data'>";
for(
$i=0;$i<$numoffile;$i++) {
    print 
"<input type='file' name='myfiles[]' size='30'><br>";
}
print 
"<input type='submit' name='action' value='Upload'>";
print 
"</form>";

?>
But if you must pass each file name to a different page for this task (maybe among with other operations), then you can have this instead of the mysql_query() line:

PHP Code:

$my_array[] = $_FILES['myfiles']['name'][$i]; 

In the end of the loop, you get an array $my_array that contains all the filenames that are uploaded. Then you can pass this array in SESSION, GET, or even POST (with curl), etc, to another page where you do your DB stuff.

I don't know - does it help?
__________________
Blavv =|
Reply With Quote
  #5 (permalink)  
Old 03-27-04, 04:01 AM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks blaw

Its morning here now not night and the wife wants to go out shopping so I will have to try this out later.

This here is the first part of a script and I will put them all together when I get all the bits to work.

The script will - upload the files to the host ( bit you can see here ) - make a thumbnail, low qulity watermarked version and a normal version ( imagemagick ). The uploaded file will be deleted ( as it can be any size file ) usink Unlink as well as a tempory version of the file ( need to get from uploaded file to low qulity watermarked version ).

These files then will be available for searching and inserting into tables using different settings set by the user in a drop down list.

I think I will use your second method as I need to insert some "text, "date", and who the photo is of into the database as well and I can then do all that straight away on a page when uploading rather than go into the database later and edit.

Thanks Again for the help Anthony
Reply With Quote
  #6 (permalink)  
Old 03-27-04, 08:25 AM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
Bit more help please

Its taking me hours to sort the simplest things !!

I have this bit of code still that adds the pics to the server and I have added a couple of " session " bits. this now puts something like "image.jpg" into the session that I can now get in the second script.

PHP Code:

session_start();


  
$numoffile 5;
  
  
$file_dir  "/upload/up/";
  
  if (
$_POST) {
    for (
$i=0;$i<$numoffile;$i++) {
      if (
trim($_FILES['myfiles']['name'][$i])!="") {
        
$newfile $file_dir.$_FILES['myfiles']['name'][$i];
        
move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile);
        
// Below added to create an arrar of file names to be used to input other data on page
         
$my_array[] = $_FILES['myfiles']['name'][$i];
        
$j++;
      }
    }
  }
  if (isset(
$j)&&$j>0) print "Your file(s) has been uploaded.<br>";
  print 
"<form method='post' enctype='multipart/form-data'>";
  for(
$i=0;$i<$numoffile;$i++) {
    print 
"<input type='file' name='myfiles[]' size='30'><br>";
  }
  print 
"<input type='submit' name='action' value='Upload'>";
  print 
"</form>";

 
$_SESSION['name_array'] = $my_array
I now have a second bit of script that takes the values from the array in the session and populates the " name " box of the form.

All seems OK down to here and here are my problem's :

1/ When I upload the variables to this table and Mysql I want "image" not "image.jpg". Some where I need - $name_array=substr($name_arrary, 0, -4); - ?

2/ How do I write the values to be uploaded ? ( blaw did an example but I am not sure how it worked ).

3/ Do I need to unset the session or something ?

4/ I am sure there was something else but I cant think what it was.

PHP Code:

session_start();


// I will put connect to database etc. here

for ( $count 0$count<=4$count++ )
{
echo 
"<form = 'post' action = sql.php>";
echo 
'Name :<input size = 10 type = "text" name = "name" value =',($_SESSION['name_array'][$count]),'><br>';
echo 
"Year :<input size = 10 type = 'text' name = 'year'><br> ";
echo 
"MMDD :<input size = 10 type = 'text' name = 'date'><br>  ";
echo 
"Person :<input size = 10 type = 'text' name = 'person'><br><br>";

// Mysql bit

mysql_query("INSERT INTO photos (name, year, date, person) VALUES ('".$_FILES['myfiles']['name'][$i]."')");

Thanks Anthony
Reply With Quote
  #7 (permalink)  
Old 03-27-04, 09:24 AM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
Remembered

I have just remembered what the other thing was.

With the second script above it :

1/ Puts 5 lots of forms if there is any data to input or not - would be good if it only showed the ones with a value in $name_array.

Sorted this one
PHP Code:

$total count($_SESSION['name_array']);

//echo $total;

for ( $count 0$count<$total$count++ ) 
2/ It also would be nice to have one on the screen at a time and when you pressed submit it went to the next.

3/ Also I CANT submit the details !!!!!

Do not want a lot do I ?

Anthony

Last edited by Bonzo; 03-27-04 at 09:30 AM.
Reply With Quote
  #8 (permalink)  
Old 03-27-04, 10:49 AM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
3/ Above

This is sort of working OK - the problem is that when the page loads it sends the data it has - name in this case - to the databas before I can input any more details.

Anthony
Reply With Quote
  #9 (permalink)  
Old 03-27-04, 11:04 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
Quote:
I blame NeverMind for this problem LOL
well, after you read the following lines, you will blame me more

Quote:
1/ When I upload the variables to this table and Mysql I want "image" not "image.jpg". Some where I need - $name_array=substr($name_arrary, 0, -4); - ?
yes, but if it was an array simply use foreach()
PHP Code:

foreach($my_array as $my_array) {

  
$my_array substr($my_array0, -4);

Quote:
3/ Do I need to unset the session or something ?
no need for that, because once the browser is closed the session is destroyed ..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #10 (permalink)  
Old 03-27-04, 02:28 PM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
Diving me MAD

I am on the one step forward about 5 steps back stage at the moment.

PHP Code:

session_start();


//db connection here

echo  "<center><h1>Input information for uploading to database</h1>";

// Count number of values in the array
$total count($_SESSION['name_array']);
//echo $total;

// Only loop for the amount of vallues in the array
for ( $count 0$count<$total$count++ )

{
$my_array substr(($_SESSION['name_array'][$count]), 0, -4);
echo 
"<table>";
echo  
"<tr><td>";
echo 
"<form = 'post' action = '3.php'>";
echo 
"<br>Name :<input size = '10' type = 'text' name = 'name' value ='",($my_array),"'<br>";
echo 
"<br>Year : <input size = 10 type = 'text' name = 'year'> ";
echo 
" MMDD : <input size = 10 type = 'text' name = 'date'> ";
echo 
" Person : <input size = 10 type = 'text' name = 'person'>  ";
echo 
"<br>Text : <input size = 100 type = 'text' name = 'text'><br>";
echo 
"<br><input type = 'submit' value='submit'>";
echo 
"</form>";
echo 
"</table>";
}

echo 
mysql_error();
mysql_close();

echo  
"</center>"
The script above creats a table which when submitted adds the data to the database.

It gets the name_array from a session and works OK.

There are up to 5 items in the array ( current setting ) and so I have it set to loop through the actual qty of items creating tables.

Problem :It puts all the tables on the same page but only updates the one when submit pressed !!

I have tried all sorts of things without sucess; can anyone help me out ?

Anthony

P.S. I dont know what I would do without you NeverMind - notice I am using one of your sugestions here. I could not get the foreach to work though.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting Rated relledge Announcements 0 06-07-03 04:55 PM


All times are GMT -5. The time now is 10:21 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.