SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Make a directory based on the $filename

    Hi everyone,
    I'm looking for any solution that help me solves this problem.
    In fact, I've got a script that allows my customers to upload files on my server (you can see an example at this link http://lineatureftp.fr/upload/SimpleUpload.html). But I would like that when my customer upload a file, this file will going into a newly create directory (name based on the filename of the upload file).
    My code is:
    #!/usr/bin/perl -w


    use CGI;




    print "Upload resultbr>"; # At least one symbol should be sent to response!!!

    $upload_dir = "upload/";
    $query = new CGI;

    print $query->header ( );
    print $query->start_html();
    @names = $query->param;
    foreach $param (@names) {
    my $filename = $query->param($param);
    $filename =~ s/.*[\/\\](.*)/$1/;
    my $upload_filehandle = $query->upload($param);

    if(!defined($upload_filehandle))
    {
    print "can't open param $filename";
    }
    else
    {
    open UPLOADFILE, ">$upload_dir/$filename";
    binmode UPLOADFILE;
    while ( <$upload_filehandle> )
    {
    print UPLOADFILE;
    }
    close UPLOADFILE;
    print "file \"$filename\" uploaded";
    }
    print $query->br();
    }

    print "--------------";
    print $query->end_html();
    but I don't found any solution to mkdir a new directory with the name of the "upload file".
    Can someone help me?
    Thanks
    Jerome

  2. #2
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you doing this to avoid some type of problem with duplicate filenames or something? It just doesn't make sense to create a new directory for each image. But you can use mkdir() to make a dir.

    http://perldoc.perl.org/functions/mkdir.html

  3. #3
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No in fact I would like to have a folder just for having a better organization (otherwise the folder will be full of uploads files).
    I'm trying with the mkdir but i didn't find any solution to have the directory created with the $filename...

  4. #4
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found a solution that make a folder using the $filename but...
    the problem now is that EACH file uploaded is going into a newly create folder. Is there any solution that all files uploaded at the same time are going into just one folder.
    Here the script I use:
    #!/usr/bin/perl -w

    use CGI;

    print "Upload resultbr>"; # At least one symbol should be sent to response!!!

    $query = new CGI;

    $filename =~ s/.*[\/\\](.*)/$1/;

    print $query->header ( );
    print $query->start_html();
    @names = $query->param;
    foreach $param (@names) {
    my $filename = $query->param($param);

    #! try to create directory
    $filename =~ s/.*[\/\\](.*)/$1/;
    $upload_dir2 = "upload/$filename";
    unless (-d "$upload_dir2"){
    mkdir ("$upload_dir2", 0777);
    chmod(0777, "$upload_dir2");
    }
    #! end of create directory


    $filename =~ s/.*[\/\\](.*)/$1/;

    my $upload_filehandle = $query->upload($param);

    if(!defined($upload_filehandle))
    {
    print "can't open param $filename";
    }
    else
    {
    open UPLOADFILE, ">$upload_dir2/$filename";
    binmode UPLOADFILE;
    while ( <$upload_filehandle> )
    {
    print UPLOADFILE;
    }
    close UPLOADFILE;
    print "file \"$filename\" uploaded";
    }
    print $query->br();
    }

    print "--------------";
    print $query->end_html();

  5. #5
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the problem now is that EACH file uploaded is going into a newly create folder.
    Thats what I was getting at when I said: "It just doesn't make sense to create a new directory for each image."

    The solution is aready in the first code you posted:

    Code:
    $upload_dir = "upload/";
    just point that to a different directory:

    Code:
    $upload_dir = "upload_new/";
    you may have to create that directy first though before trying to store files in it via your form.

  6. #6
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First thank you KevinR, I agree with you. The problem is that I want that when my customers upload their files the script create a new directory and store all files in.
    I try to make some fields but the variable is not read in the cgi.
    My upload page is like:
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Simple upload example</title>
    </head>
    <body>
    All files with examples should be opened from web server (i.e http://www.yourserver.com/example.html or http://localhost/example.html)!<br><BR>
    Simple upload example. By default Flash posts files to "uploadfiles.aspx" ASP.NET file processing script. To set other script file name modify "uploadUrl" parameter value of Flash object.
    <table>
    <tr>
    <td align="center">
    <font style="font-size:10px;font-family:Verdana, Arial, Helvetica, sans-serif" color="#494949">
    You need at least 8 version of Flash player. Download last version
    <a target="_blank" href="http://www.adobe.com/shockwave/download/alternates/">here</a>
    </font><br>
    <OBJECT id="FlashFilesUpload" codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"
    width="450" height="350" classid="clsid27CDB6E-AE6D-11cf-96B8-444553540000" VIEWASTEXT>
    <!-- Replace symbols " with the &quot; at all parameters values and
    symbols "&" with the "%26" at URL values or &amp; at other values!
    The same parameters values should be set for EMBED object below. -->
    <PARAM NAME="FlashVars" VALUE="uploadUrl=unixuploadtest.pl
    &loaderMode=No
    &downloadMode=No
    &useExternalInterface=No
    &alertAboutOldversion=Yes
    &filesToDownload=
    &imageToLoad=
    &redirectUploadUrl=upload/result2.php
    &maxFileSize=-1
    &maxFileCount=-1
    &maxFileSizeTotal=-1
    &fileTypes=All files|*.*;Images|*.jpg;*.jpeg;*.gif;*.png;*.bmp>
    <PARAM NAME="BGColor" VALUE="#F8F6E6">
    <PARAM NAME="Movie" VALUE="ElementITMultiPowUpload1.6.swf">
    <PARAM NAME="Src" VALUE="ElementITMultiPowUpload1.6.swf">
    <PARAM NAME="WMode" VALUE="Window">
    <PARAM NAME="Play" VALUE="-1">
    <PARAM NAME="Loop" VALUE="-1">
    <PARAM NAME="Quality" VALUE="High">
    <PARAM NAME="SAlign" VALUE="">
    <PARAM NAME="Menu" VALUE="-1">
    <PARAM NAME="Base" VALUE="">
    <PARAM NAME="AllowScriptAccess" VALUE="always">
    <PARAM NAME="Scale" VALUE="ShowAll">
    <PARAM NAME="DeviceFont" VALUE="0">
    <PARAM NAME="EmbedMovie" VALUE="0">
    <PARAM NAME="SWRemote" VALUE="">
    <PARAM NAME="MovieData" VALUE="">
    <PARAM NAME="SeamlessTabbing" VALUE="1">
    <PARAM NAME="Profile" VALUE="0">
    <PARAM NAME="ProfileAddress" VALUE="">
    <PARAM NAME="ProfilePort" VALUE="0">

    <!-- Embed for Netscape,Mozilla/FireFox browsers support. Flashvars parameters are the same.-->
    <!-- Replace symbols " with the &quot; at all parameters values and
    symbols "&" with the "%26" at URL values or &amp; at other values! -->
    <embed bgcolor="#F8F6E6" id="EmbedFlashFilesUpload" src="ElementITMultiPowUpload1.6.swf" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="450" height="350"
    flashvars="uploadUrl=unixuploadtest.pl
    &loaderMode=No
    &downloadMode=No
    &useExternalInterface=No
    &alertAboutOldversion=Yes
    &filesToDownload=
    &imageToLoad=
    &redirectUploadUrl=upload/result2.php
    &maxFileSize=-1
    &maxFileCount=-1
    &maxFileSizeTotal=-1
    &fileTypes=All files|*.*;Images|*.jpg;*.jpeg;*.gif;*.png;*.bmp>
    </embed>
    </OBJECT>

    </td>
    </tr>
    </table>
    </body>
    </html>
    Could you help me to make the form?
    Thanks a lot!
    Jerome

    PS: you can see the page directly at http://lineatureftp.fr/upload/SimpleUpload.html

  7. #7
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what you want is a file manager script or a web host script. That would allow people to signup with a name and password and store files in their user directory.

  8. #8
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No I don't need to have a web host script because I already have sub-domain for each of my customers. In fact I just need to have a new folder in their upload_dir because they send me a lot of files for each work.
    I try to put this code in my html page:
    <form>
    Form fieldsbr>
    <input type="text" name="FirstName" size="18" maxlength="30">
    <input name="submitbtn" onClick="mysubmit();" type="submit" value="Upload" >
    </form>
    and this in the .pl file but it don't function:
    $name = $_GET['FirstName'];
    #! try to create directory
    $name = $_GET['FirstName'];
    $upload_dir2 = "upload/$name";
    unless (-d "$upload_dir2"){
    mkdir ("$upload_dir2", 0777);
    chmod(0777, "$upload_dir2");
    }
    #! end of create directory
    Is there something I miss?

  9. #9
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, what are you using, PHP or perl?

  10. #10
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm using perl because with php i've got a limit with the upload size…

  11. #11
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the last code you posted is using PHP variables to try and get the form data. Your other code above uses perl and the CGI module to get the form data.

    Based on the 4th post in this thread you could do this:

    Code:
    my $name = $query->param('FirstName');
    my $upload_dir2 = "upload/$name";
    unless (-d $upload_dir2){
       mkdir ($upload_dir2, 0777);
       chmod(0777, $upload_dir2);
    }
    but its a bad idea to allow unvalidated user input for things like folder names. The very first rule of CGI programming is:

    never trust input data

    the second rule is:

    see first rule

  12. #12
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Finally I replace the form field name by a field based on the date: it will be more usefull I think but I always got a problem:
    in fact I see that on the code of the html file it's write:
    <form onChange="window.alert('hi');" method="get" onSubmit="return mysubmit();" id="myform" name="myform" action="bla.aspx">
    and
    <input name="submitbtn" onClick="mysubmit();" type="submit" value="Upload" >
    but if I replace "bla.aspx" by the .pl file (unixupload.pl) I always got an error:
    can't open param textintheformfield
    can't open param
    can't open param Upload
    I contact the author of the solution but have you got an idea how to correct this?

  13. #13
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know, you are using a flash CGI form, which is something I am not familiar with at all.

  14. #14
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for all your post, I hope that the author have a solution.
    Regards,
    Jerome


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •