SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Huntington Beach, CA
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Upload script help

    I have been working on a file upload script and I can not seem to get it to work properly. I am creating a directory which corresponds to an order ID (the eCommerce software is ZenCart). I have that part done and working properly.

    Then I want to pass the new order directory to a new page that picks up the upload directory from a form (I have this done as well)

    I run into the problem when the script actually tries to upload the file. It will upload it, but not to the directory I need it to.

    This is the script that creates the directory adn puts it in a form to pass to the upload page:

    Code:
    $upload_dir = "customer_files/$zv_orders_id";
    mkdir($upload_dir, 0777);
    
    ?>
    <!--eof -product notifications box-->
    
    <!--bof -file upload box -->
    <table>
    	<tr>
    		<td align="center">
    		<? echo $upload_dir; ?>
    		<form action="index1.php" method="post">
    			<input type="hidden" name="dir" value="<? echo $upload_dir; ?>" />
    			<input type="submit" value="Please click here to upload files!" />
    		</form>
    		</td>
    	</tr>
    </table>
    <!-- eof -file upload box -->
    This is the upload script, it uploads to the document root and not the upload directory.
    Code:
    <?php
    //set these variables-----------------------------------------------------------------
    $dir = $_POST['dir'];   //path to your targetfolder after your domain
    
    include ($_SERVER['DOCUMENT_ROOT']."/new_store/upload_class.php"); //classes is the map where the class file is stored (one above the root)
    
    $max_size = 1024*250; // the max. size for uploading
    	
    $my_upload = new file_upload;
    
    $my_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/$dir"; // "files" is the folder for the uploaded files (you have to create this folder)
    $my_upload->extensions = array(".png", ".zip", ".pdf"); // specify the allowed extensions here
    // $my_upload->extensions = "de"; // use this to switch the messages into an other language (translate first!!!)
    $my_upload->max_length_filename = 50; // change this value to fit your field length in your database (standard 100)
    $my_upload->rename_file = false;
    		
    if(isset($_POST['Submit'])) {
    	$my_upload->the_temp_file = $_FILES['upload']['tmp_name'];
    	$my_upload->the_file = $_FILES['upload']['name'];
    	$my_upload->http_error = $_FILES['upload']['error'];
    	$my_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "n"; // because only a checked checkboxes is true
    	$my_upload->do_filename_check = (isset($_POST['check'])) ? $_POST['check'] : "n"; // use this boolean to check for a valid filename
    	$new_name = (isset($_POST['name'])) ? $_POST['name'] : "";
    	if ($my_upload->upload($new_name)) { // new name is an additional filename information, use this to rename the uploaded file
    		$full_path = $my_upload->upload_dir.$my_upload->file_copy;
    		$info = $my_upload->get_uploaded_file_info($full_path);
    		// ... or do something like insert the filename to the database
    	}
    }
    ?> 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Upload example</title>
    <style type="text/css">
    <!--
    label {
    	float:left;
    	display:block;
    	width:120px;
    }
    input {
    	float:left;
    }
    -->
    </style>
    </head>
    
    <body>
    <h3>File upload script:</h3>
    <p>Max. filesize = <?php echo $max_size; ?> bytes.</p>
    <p><? echo $dir; ?></p>
    <p><? echo $my_upload->upload_dir; ?></p>
    <form name="form1" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
      <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_size; ?>"><br>
      <label for="upload">Select a file...</label><input type="file" name="upload" size="30"><br clear="all">
      <label for="name">New name?</label><input type="text" name="name" size="20"> 
      (without extension!) <br clear="all">
      <label for="replace">Replace ?</label><input type="checkbox" name="replace" value="y"><br clear="all">
      <label for="check">Validate filename ?</label><input name="check" type="checkbox" value="y" checked><br clear="all">
      <input style="margin-left:120px;" type="submit" name="Submit" value="Submit">
    </form>
    <br clear="all">
    <p><?php echo $my_upload->show_error_string(); ?></p>
    <?php if (isset($info)) echo "<blockquote>".nl2br($info)."</blockquote>"; ?> 
    </body>
    </html>
    I do not know what to do. Any help is appreciated!
    Rob
    Pier Marketing ◄ Printing and Marketing Collateral - Order Online
    Pier Technologies ◄ Web Development & IT Solutions
    And That's News? ◄ My NEW Blog!!

  2. #2
    SitePoint Enthusiast Sven S.'s Avatar
    Join Date
    May 2005
    Location
    Hamburg, Germany
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Rob,

    I'm not really sure if this is really the problem but take a look at your second script which should upload the file.
    There it defines $dir like this:
    PHP Code:
    $dir $_POST['dir'];   //path to your targetfolder after your domain 
    The problem is that there is no field in the form that transfers the name of the directory. That's why
    PHP Code:
    $my_upload->upload_dir $_SERVER['DOCUMENT_ROOT']."/$dir"
    consits just of the DOCUMENT_ROOT: $dir is empty.

    To fix the bug you just have to add a hidden field in your form that transmits the name of the dir:
    Code:
    <input type="hidden" name="dir" value="<?php echo $my_upload->upload_dir; ?>" />
    This code supposes that the first form you posted leads to the second one, otherwise $my_upload->upload_dir is empty because $_POST['dir'] is empty then.

    I hope I understood your problem. If you have any more questions feel free to ask!

    Sven Schoene

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Huntington Beach, CA
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your reply.

    I have this in my page that actually generates the directory:

    Code:
    <input type="hidden" name="dir" value="<? echo $upload_dir; ?>" />
    I have also made it so the upload directory echo's on the page so I am sure it is being passed.

    I am really confused on this one.
    Rob
    Pier Marketing ◄ Printing and Marketing Collateral - Order Online
    Pier Technologies ◄ Web Development & IT Solutions
    And That's News? ◄ My NEW Blog!!

  4. #4
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WebRob View Post
    Thank you for your reply.

    I have this in my page that actually generates the directory:

    Code:
    <input type="hidden" name="dir" value="<? echo $upload_dir; ?>" />
    I have also made it so the upload directory echo's on the page so I am sure it is being passed.

    I am really confused on this one.
    veiw the source code on the web page and confirm that <? echo $upload_dir; ?> is actually echoing the correct directory.


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
  •