cant upload file properly

Discussion in 'PHP' started by jacey, Apr 9, 2006.

  1. #1
    The filename for file to be uploaded is different from the one uploaded.
    Eg:

    the file to be uploaded is : DSS_Chapter_12 f f___h.pdf
    I renamed it in php code as : dss_chapter_12_f_f_h.pdf
    file uploaded name: dss_chapter_12

    My code:

    $sourceFile = $_FILES["sourceFile"]["name"];
    $target_path = "thesis/"
    $restrictChar=array("/","\\",":","?","\"","<",">","|",",","\.","*"," ","(",")","&","'");
    $sourceFile = trim(strtolower($sourceFile));
    $sourceFile = trim(str_replace($restrictChar,"_",basename( $sourceFile)));
    $sourceFile = FilterArray(basename($sourceFile));
    $target_path .= basename($sourceFile);

    $sourceFileTemp =$_FILES['sourceFile']['tmp_name'];
    if(move_uploaded_file($sourceFileTemp, $target_path))
    {
    if (is_uploaded_file($sourceFile))
    {
    echo "File ". $sourceFile ." uploaded successfully.\n";
    echo "Displaying contents\n";

    }
    else
    {
    echo "error!";
    }
    }

    function FilterArray($myArray)
    {
    for($j=1;$j<strlen($myArray);$j++)
    { $k=$j-1;
    if(($myArray[$j] == "_")&&($myArray[$k]=="_"))
    {
    $myArray[$k] ="";
    }


    }
    return $myArray;
    }
     
    jacey, Apr 9, 2006 IP
  2. SecondV

    SecondV Active Member

    Messages:
    76
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    70
    #2
    Try replacing spaces in filenames as _
    
    $sourceFile = $_FILES["sourceFile"]["name"];
    $target_path = "thesis/"
    $restrictChar = array("/", "\\", ":", "?", "\"", "<", ">", "|", ",", "\.", "*", " ", "(", ")", "&", "'", " ");
    $sourceFile = trim(strtolower($sourceFile));
    $sourceFile = str_replace($restrictChar, "_", basename($sourceFile));
    $sourceFile = FilterArray(basename($sourceFile));
    $target_path .= basename($sourceFile);
    
    $sourceFileTemp = $_FILES['sourceFile']['tmp_name'];
    
    if(move_uploaded_file($sourceFileTemp, $target_path))
    {
      if(is_uploaded_file($sourceFile))
      {
        echo "File ". $sourceFile ." uploaded successfully.\n";
        echo "Displaying contents\n";
      }
      else
      {
        echo "error!";
      }
    }
    
    function FilterArray($myArray)
    {
      for($j = 1; $j < strlen($myArray); $j++)
      {
        $k = $j-1;
        if(($myArray[$j] == "_") && ($myArray[$k]=="_"))
        {
          $myArray[$k] = "";
        }
      }
      return $myArray;
    }
    
    PHP:
     
    SecondV, Apr 12, 2006 IP