Help with displaying the image link/path

Discussion in 'PHP' started by chrisj, Oct 4, 2014.

  1. #1
    I believe that when a file is selected, this code (below) moves the renamed image file to uploads/

    I'd simply like to have the renamed file's path to display on the same page, after it's moved.

    Can you provide some insight on what's needed to do that?

    
    if ($form_submitted == 'yes') {
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = strtolower( end($temp) );
    if ( $_FILES["file"]["size"] < 2000000
    && in_array($extension, $allowedExts) )
    {
    if ($_FILES["file"]["error"]!= 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    } else {
    $length = 20;
    $randomString = substr(str_shuffle(md5(time())),0,$length);
    $newfilename = $randomString . "." . $extension;
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename );
    $file_location = '<a href="www.--.com/upload/' . $newfilename . '">' . $newfilename . '</a>';
    }
    } else {
    echo "Invalid upload file";
    }
    $description = $description . " \n " . $newfilename;
    }
    
    PHP:
    The html part of this is simply:

    
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file">
    
    HTML:
     
    chrisj, Oct 4, 2014 IP
  2. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #2
    If you mean for it to reload the same page and show the result, you can use this in your code to check if the button was submitted.

    if ($_POST['submitted']){
    $newname=$_POST['newfile'];
    $url="http://www.--.com/uploads/";
    $newfilename=$url.$newname;
    
    echo "Your file is stored at $newfilename";
    } 
    PHP:
    Then you would want to add this for your submit button

     <input type="hidden" name="newfile" value="<?php echo $newfilename; ?>"> 
                        <input type="submit" name="submitted" value="Submit">
    
    HTML:
     
    KangBroke, Oct 4, 2014 IP
    Anveto likes this.
  3. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #3
    I assume you were trying to make a link here

    
    $file_location = '<a href="www.--.com/upload/' . $newfilename . '">' . $newfilename . '</a>';
    
    PHP:
    So add http:// and modify the link a bit and then echo that variable and it should show you the link.

    
    $file_location = '<a href="http://www.--.com/upload/' . $newfilename . '">http://www.--.com/upload/' . $newfilename . '</a>';
    echo $file_location;
    
    PHP:
     
    Anveto, Oct 4, 2014 IP
    KangBroke likes this.
  4. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #4
    Thanks
     
    chrisj, Oct 5, 2014 IP