Problem with upload script

Discussion in 'PHP' started by paolofru, Jun 1, 2012.

  1. #1
    Hi, I have a big problem with a php script for upload. I have searched many weeks in the web for solve this problem but i haven't find nothing about this...

    In my script i have 3 page: invio.php, makedir.php and upload.php

    in invio.php i have the form for user data:
    
    <form id="form1" name="form1" method="post" action="makedir.php" enctype="multipart/form-data">
    <input type="text" name="azienda" id="azienda2" />
    
    PHP:
    this page submit the variable azienda to makedir.php, this page create a folder on ftp with the name in the variable. I create the session called "azienda" because I need to post this variable to the page upload.php.

    <?php
    session_start();
    
    $cartella = $_POST['azienda'];
    $login = 'xxx';
    $password = 'xxx';
    
    $conn = ftp_connect('xx.xx.xx.xx') or die('Could not connect');
    @ftp_login($conn, $login, $password);
    
    $cartella = str_replace(' ', '_', $cartella);
    @ftp_mkdir($conn, $cartella);
    @ftp_chdir($conn, $cartella);
    $_SESSION['cartella'] = $cartella;
    
    ?> 
    PHP:
    In this page i have a flash uploader that called upload.php

    The page upload.php take the file and send it to the ftp server
    <?php
    <?php
    session_start();
    print_r($_SESSION);
    
    $cartella_up = $_SESSION['cartella'];
    
    if (!empty($_FILES)) {
        $tempFile = $_FILES['Filedata']['tmp_name'];
        $login = 'xyz';
        $password = 'xyz';
        
        $conn = ftp_connect('xx.xx.xx.xx') or die('Could not connect');
        @ftp_login($conn,$login,$password);
        ftp_put($conn, "{$cartella_up}/{$_FILES['Filedata']['name']}", $tempFile, FTP_BINARY);
        ftp_close($conn);
    }
    ?> 
    
    PHP:
    and here we have the problem....
    the script make the upload of files but only in the root directory, it not recognize the variable with the name of ftp directory...
    but if I specify the name of directory it work perfectly, for example if I change $cartella_up to "test1234" it work perfectly!

    have you any idea for the reasons of this problem.

    king regards
     
    paolofru, Jun 1, 2012 IP
  2. dujmovicv

    dujmovicv Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    4
    Trophy Points:
    48
    #2
    Have you checked if the variable $cartella_up is set properly? Try to echo it to see its value.
     
    dujmovicv, Jun 1, 2012 IP
  3. paolofru

    paolofru Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, i've tried to echo the variable $cartella_up and it print the exact string from the form.
     
    paolofru, Jun 1, 2012 IP
  4. kbduvall

    kbduvall Peon

    Messages:
    71
    Likes Received:
    3
    Best Answers:
    4
    Trophy Points:
    0
    #4
    This may be a silly question, but is the server you're connecting to with FTP the same server your scripts are on?
     
    kbduvall, Jun 1, 2012 IP
  5. paolofru

    paolofru Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    No, the website is hosted in a italian provider and the ftp server is a machine in the laboratory of my customer.
     
    paolofru, Jun 1, 2012 IP
  6. paolofru

    paolofru Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I'VE FOUND THE SOLUTION!!

     <?php 
    session_start(); //open session 
    
    $cartella_up = $_SESSION['cartella']; //session in a string 
        $login = 'xyz'; //log ftp 
        $password = 'xyz'; //pass ftp 
        $conn = ftp_connect('xx.xx.xx.xx') or die('Could not connect'); //ftp connection 
        ftp_login($conn,$login,$password); //ftp connection 
        $contents = ftp_nlist($conn, "."); //make the folders list 
    
    $key = array_search($cartella_up, $contents); //search the key in the array 
    $cartella_upload = $contents[$key]."/"; //add the slash for path 
    echo $cartella_upload;  //print for check 
    
    if (!empty($_FILES)) { 
        $tempFile = $_FILES['Filedata']['tmp_name']; 
      ftp_put($conn, $cartella_upload.$_FILES['Filedata']['name'], $tempFile, FTP_BINARY); //file upload 
    } 
    session_destroy(); //close the session 
    ftp_close($conn); 
    ?>  
    PHP:
    works, the only problem is that if I go back to the first page and memorize another session I will keep the first ...
    I'll try to fix it tomorrow morning ...

    YEAH ;):cool:
     
    paolofru, Jun 2, 2012 IP