Links do Not work (download problem)

Discussion in 'Programming' started by broken_mirror, Aug 14, 2006.

  1. #1
    Hi guys
    I am coding a program for free bookstore and I am uploading the book and every thing is ok till now it means the book goes to its declared physical place on the hard disk .but there is problem here ,I get a query from a database to get its name and path and put it in the < a href = > tag to make it available to download the link is available but when I click on it in Internet Explorer it goes inside the link and no content is available instead of starting the progress of download or opening Adobe Acrobat(For PDF files). It is noticeable that the url path is right I mean it is the correct name of the book and its specified path on the hard disk. On the FireFox browser it says there is no content available and shows white page.

    In any Web Browser when I right click on a link an choose save to disk the download progress start and the linked file will be saved on the hard disk but when I trying to open the saved files all of them got a damage and can not be opened.

    I have checked my operating system and browser to see any mismatches by creating a direct path to those files. But every thing is OK :O .

    Now is there any configuration regulation? I am using PHP5 and Apache2 and MySQL 4. and my code is fully object oriented.

    Thanx
     
    broken_mirror, Aug 14, 2006 IP
  2. broken_mirror

    broken_mirror Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2

    This is my code to upload the file:

    $book_dir = "book/";
    $filePath_name =$_FILES['filePath']['name'];

    $b->setPath($filePath_name);

    $filePath_tmp_name = $_FILES['filePath']['tmp_name'];
    $filePath_size = $_FILES['filePath']['size'];
    $filePath_type = $_FILES['filePath']['type'];

    if(isset($_ENV['WINDIR'])) {
    $filePath = str_replace("\\\\", "\\", $FILES['filePath']['name']);
    }
    $filename2 = basename($filePath_name);
    if($filePath_size <= 0) die ("$filename2 is empty.");
    if (!@move_uploaded_file($filePath_tmp_name, "$book_dir/$filename2"))
    die("Can't copy $filePath_name to $filename2.");
    if(isset($_ENV['WINDIR']) && !@unlink($filePath))
    die("Can't delete the file $filePath_name.");
    echo "$filename2 has been successfully uploaded.<BR>";
    echo "Filesize: " . number_format($filePath_size) . "<BR>";
    echo "Filetype" .$filePath_type."<BR>";

    $b->addBook($b);
     
    broken_mirror, Aug 15, 2006 IP
  3. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #3
    I do not know much about file handling in php, but try using copy() instead of move_uploaded_file(). The problem seems to be that the file is being created but the content of the file is not.

    I would suggest coding only the upload part and getting it to work, and then you can add all the error handling
     
    danielbruzual, Aug 15, 2006 IP
  4. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #4
    I have just made a little script that uploads .pdf files (it also uploads .zip files). I tested it in my local server and it worked just fine.

    this is the file that you use to upload the pdfs
    
    <?
    
    if(isset($_POST['s_submit'])){
    if (isset($_FILES['s_file'])){
    
    $target_path = "books/";
    $target_path = $target_path . basename( $_FILES['s_file']['name']); 
    $save = move_uploaded_file($_FILES['s_file']['tmp_name'], $target_path);
    
    
    }
    }
    ?>
    
    <table>
    <form method="post" enctype="multipart/form-data" action="">
    <tr><td>Choose File</td><td><input type="file" name="s_file"></td></tr>
    <tr><td></td><td><input type="submit" name="s_submit"></td></tr>
    </form>
    </table>
    
    Code (markup):
    this is the file that you use to display the pdfs in the directory
    
    <?
    $books_dir = "books/";
    $handle = opendir($books_dir);
    $filelist = "";
    
    
    
    while ($file = readdir($handle)){
    		if(!is_dir($file) && !is_link($file)){
    			if(strpos(strtolower($file), ".pdf")){
    
    				$filelist .= "<a href=\"".$books_dir.$file."\">".preg_replace("/\.pdf$/", "", $file)."</a>";
    				$filelist .= "<br />";
    					
    			}
    		}
    	}
    
    echo $filelist;
    
    ?>
    
    
    
    Code (markup):
     
    danielbruzual, Aug 15, 2006 IP