0

Hey everyone.
I have a problem. I'm working on a site which let users upload files to the server. It works like this, I have created a php file to handle the uploading and when the uploading is successful it redirects back to the page where the form is located. But the problem is that I'm trying to get the page to update so it displays the newly uploaded file, but I can't seem to manage this. I've tried using sessions but with no avail.
Does anyone have any advice please?
I would appreciate it
Thanks

Edited by Sterotech: Spelling mistake

2
Contributors
5
Replies
41
Views
4 Years
Discussion Span
Last Post by pixelsoul
0

My first piece of advice would be to post your code so we have an idea on how it works. There are many different ways of doing something.

0

Okay, this is the form...

<form enctype="multipart/form-data" action="upload.php" method="post">
    <input type="file" name="file" id="file" /></br />
    <input type="submit" value="Upload" />
</form>

and this is the php code to check whether the file has a valid extension, whether it already exists or not and then uploads it... There is nothing wrong with the code, I just want to know how I can update the webpage to display the new uploaded item in a list with old items. BTW: this php is in a seperate file than the form...

<?php
    $aExt = "pdf";
    $ext = end(explode('.', $_FILES['file']['name']));
    if($ext==$aExt) {
        if($_FILES['file']['error'] > 0) {
            echo "<script>alert('Error ocurred during upload! Try again.');</script>";
        }
        else {
            if(file_exists("tutorials/".$_FILES['file']['name'])) {
                echo "<script>alert('File exists.');</script>";
            }
            else {
                move_uploaded_file($_FILES['file']['tmp_name'], "tutorials/".$_FILES['file']['name']);
                echo "<script>alert('File upload successful.');</script>";
            }
        }
    }
    else {
        echo "<script>alert('Invalid File.');</script>";
    }

    header("Location: localhost/index.php");
    exit;
?>
2

Okay, so... Your method was behaving a little strange. I usually wouldn't have written it that way so I was probably just overlooking something I am not used accounting for. So, I decided to just write this up the way I would have if I did it. Hopefully it is something you can use. If not, then hopefully it helps give you an idea on how to do it better or differently. I would have actually written this all as one page that submitted the upload to it self.

<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>

    <!-- Basic Page Needs -->
    <meta charset="utf-8">
    <title></title>
    <meta name="author" content="" />
    <meta name="description" content="" />

</head>
<body>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="file" name="file" id="file" /></br />
    <input type="submit" name="submit" value="Upload" />
</form>

<?php

if(isset($_POST['submit'])){ // if form submitted

    $aExt = "pdf";
    $ext = end(explode('.', $_FILES['file']['name']));
    $_FILES['file']['name'] = str_replace(" ","_",$_FILES['file']['name']); // replaces the spaces in the file name with underscores.

    if($ext==$aExt) {
        if($_FILES['file']['error'] > 0) {
            echo "<script>alert('Error ocurred during upload! Try again.');</script>";
        }
        else {
            if(file_exists("tutorials/".$_FILES['file']['name'])) {
                echo "<script>alert('File exists.');</script>";
            }
            else {
                move_uploaded_file($_FILES['file']['tmp_name'], "tutorials/".$_FILES['file']['name']);
                echo "<script>alert('File upload successful.');</script>";

                // Didn't need to store it in a session but just in case you wanted that
                $_SESSION['uploadPath'] = $_FILES['file']['name'];

                echo "<a href=tutorials/" . $_SESSION['uploadPath'] ." >" . $_SESSION['uploadPath'] ."</a>";

                // link to file not stored in session
                //echo "<a href=tutorials/" . $_FILES['file']['name'] ." >" . $_FILES['file']['name'] ."</a>";

            }
        }
    }
    else {
        echo "<script>alert('Invalid File.');</script>";
    }

}  

?>

</body>
</html>

Hope this helped.

0

Thanks alot, that helped. I tried posting it to itself at first but it didn't work because I did this

<form enctype="multipart/form-data" action="index.php" method="post">...

instead of

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

so thanks, I just learned something new :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.