Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

upload files to a directory

Status
Not open for further replies.

there

Programmer
Feb 4, 2002
15
CA
Hi,

I went over the the posting in this thread and TRIED and TRIED to make the coding work. I must be doing something wrong. PLEASE HELP.

in the html coding
------------------
<form action=&quot;script_name.cgi&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;>
<input type=&quot;file&quot; name=&quot;logo&quot; size=&quot;20&quot;>

in perl coding
--------------
##--------
# uploadImage()
##--------
sub uploadImage
{

$upload_logo = $field(&quot;logo&quot;);

$abs_file_to_create .= &quot;/location_on_the_web_server/&quot;;
$abs_file_to_create .= &quot;tempfile&quot;;

open(NEW_FILE,&quot;>$abs_file_to_create&quot;) || die &quot;Can't open $abs_file_to_create for output: $!&quot;;
$total_size = 0;
while ($size = read($upload_logo, $data, 8192))
{
print NEW_FILE $data;
$total_size += $size;
}
close NEW_FILE;

}

Problem I am having
-------------------
it saves the file in the right location (location_on_the_web_server) but calls the file &quot;tempfile&quot;. I would like each entry to have their own file name, example: logo.gif. and logged in each database.

PLEASE HELP. I would love to know where I went wrong
Thank you for your knowledge, wisdom and time!

There
 
The reason it's putting every file in the same filename is the line which reads:

$abs_file_to_create .= &quot;tempfile&quot;;


You're appending a string literal. You might get better results were you to append the value of a variable instead. ______________________________________________________________________
TANSTAAFL!
 
Thank you for your response.

for the line you've mentioned above
$abs_file_to_create .= &quot;tempfile&quot;;


I should go ????
$abs_file_to_create .= &quot;$logo&quot;;

note:
$logo is the value of the input field

Thank you for your knowledge, wisdom and time!

There
 
sleipnir214,

any response for the above? I have typed it below for your convenience.

for the line you've mentioned above
$abs_file_to_create .= &quot;tempfile&quot;;


I should go ????
$abs_file_to_create .= &quot;$logo&quot;;

note:
$logo is the value of the input field
Thank you for your knowledge, wisdom and time!

There
 
Actually, you will need to do some converting of $logo before you can extract the filename. Basically, when you upload a file, the input type File will display the entire directory structure to get to the file on the user's computer. Becuase of this, you need to split on the \ or / depending on the user's OS, and only take the last element of the split array as the file name.

Something similar to the code below.

Code:
$upfile = lc(join '', split / /,(split /\\/, param(&quot;upload&quot;))[-1]);

- Rieekan
 
Rieekan,

thank you for your input. I've tried what you've suggested and it didn't work. I must be doing something wrong. please look at the code below, including your suggestion inserted.

##--------
# uploadImage()
##--------
sub uploadImage
{

$upfile = lc(join '', split / /,(split/\\/, param(&quot;logo&quot;))[-1]);

$abs_file_to_create .= &quot;/location_of_directory/image&quot;;
$abs_file_to_create .= &quot;tempfile&quot;;

open(NEW_FILE,&quot;>$upfile&quot;) || die &quot;Can't open $abs_file_to_create for output: $!&quot;;
$total_size = 0;
while ($size = read($upload_logo, $data, 8192))
{
print NEW_FILE $data;
$total_size += $size;
}
close NEW_FILE;

}


Problems I am having
--------------------
- does not upload the file from desktop to web server
- does not keep the same name of the file from the desktop
- in the database, the code has the url from the desktop and not the file that should be on the web server

Your wisdom please. Thanks Thank you for your knowledge, wisdom and time!

There
 
Rieekan,

I got it somewhat working. it transfer the file from desktop to web server but the size is 0 and I am not sure it is transferred in binary (if this is a worry).

code
----
##--------
# uploadImage()
##--------
sub uploadImage
{

$upfile = lc(join '', split / /,(split/\\/, param(&quot;logo&quot;))[-1]);

$abs_file_to_create .= &quot;/location_on_web_server/&quot;;
$abs_file_to_create .= &quot;$upfile&quot;;

open(NEW_FILE,&quot;>$abs_file_to_create&quot;) || die &quot;Can't open $abs_file_to_create for output: $!&quot;;
$total_size = 0;
while ($size = read($upload_logo, $data, 8192))
{
print NEW_FILE $data;
$total_size += $size;
}
close NEW_FILE;

}

Problem
-------
in the database, it has the url from the desktop and not the web server. How can I change this?


Thank you for your knowledge, wisdom and time!

There
 
Dear wise-one,

I am getting close but still need your wisdom to solve my problem.

ADD FEATURE
I want to add an upload feature to:

add code
--------
##--------
# uploadImage()
##--------
sub uploadImage
{

$upfile = lc(join '', split / /,(split/\\/, param(&quot;logo&quot;))[-1]);

$abs_file_to_create .= &quot;/usr/local/etc/httpd/vhosts/$abs_file_to_create .= &quot;$upfile&quot;;

open(NEW_FILE,&quot;>$abs_file_to_create&quot;) || die &quot;Can't open $abs_file_to_create for output: $!&quot;;
binmode NEW_FILE;
binmode $upfile;
while ($size = read($upload_logo, $data, 8192))
{
print NEW_FILE $data;
}
close NEW_FILE;

}

problem
-------
when it uploads the file, the size is 0. How can I solve this with the code above


EDIT FEATURE
I want visitors to go back and edit their entry would the code be similar above but won't it need to check to see if the database has changed from when it was added to edited? How can I do this? Or do have a better solution?


DELETE FEATURE
When the visitor deletes their account, how can get Perl to delete the uploaded file from the server?

I would love your input!


Thank you for your knowledge, wisdom and time!

There
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top