Jump to content

Image upload code specific part of where image displays?


Html

Recommended Posts

Hi,

Where does the image uploaded here in this, display once it has been uploaded to a directory

<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP File Upload</title>
</head>
<body>
<form method="POST" action="<?php $_SERVER[ 'PHP_SELF' ] ?>" enctype="multipart/form-data">
Select an image to upload :
<input type="file" name="image" >
<input type="submit" value="Upload Image" >
</form>   
<?php
if( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' )
{
  $name = $_FILES[ 'image' ][ 'name' ] ;
  $temp = $_FILES[ 'image' ][ 'tmp_name' ] ;
  $size = $_FILES[ 'image' ][ 'size' ] ;
  $ext = pathinfo( $name , PATHINFO_EXTENSION ) ;
  $ext = strtolower( $ext ) ;
  if( $ext != 'png' && $ext != 'jpg' && $ext != 'gif' )
  { echo 'Format must be PNG, JPG, or GIF' ; exit() ; }  
  if( $size > 512000)
  { echo 'File size must not exceed 500Kb' ; exit() ; }
  if( file_exists( $name ) )
  { echo 'File '.$name.' already uploaded' ; exit() ; }
  try
  {
    move_uploaded_file( $temp , $name ) ;
    echo 'File uploaded : '.$name ;
    echo '<br><img src="'.$name.'">' ;
  }
  catch( Exception $e )
  { echo 'File upload failed!' ; }
} 
?> 
</body>
</html>

I have something a little similar, which was probably helped by a few on here last year. That works as is, but I'm curious to know where exactly, the image is temporarily displayed in this code.

While this code is structured for uploading to the main directory, and not a specific folder, it does display the image.

{
    move_uploaded_file( $temp , $name ) ;
    echo 'File uploaded : '.$name ;
    echo '<br><img src="'.$name.'">' ;
  }

Could be this?

Link to comment
Share on other sites

It is giving a temp name such as tmp000, which is stored in the set directory in php.ini, this tmp name is accessed by

 $temp = $_FILES[ 'image' ][ 'tmp_name' ] ;

This can then be checked if it is valid, it can then attempt  to move the temp file, to a specific destination with its original name from

$name = $_FILES[ 'image' ][ 'name' ] ;

Or modified name with time stamp for instance, or completely different name.

 

 

Link to comment
Share on other sites

The set directory, being the upload_tmp one.

So what about simply display images that have been sent to a folder in the directory, how can the images simply be displayed on a page, they'd simply be listed as a row going down, I am guessing without any CSS.

Link to comment
Share on other sites

I searched on bing.

<?php
 
$fileList = glob('test/*');
foreach($fileList as $filename){
    //Use the is_file function to make sure that it is not a directory.
    if(is_file($filename)){
        echo $filename, '<br>'; 
    }   
}

https://thisinterestsme.com/php-list-all-files-in-a-directory/

Link to comment
Share on other sites

I don't know why so many people want to use glob(). Most of the time there is no need. scandir() is more efficient (though you have to add a bit of code to filter out "." and ".." from the directory list)

Link to comment
Share on other sites

why not? FOR IMAGE BROWSING, DAMN, THAT WAS LONG
<!DOCTYPE HTM>
<head>
<title>PHP File Upload</title>
</head>
<body>  
<?php>
<input aria-label="Add Photo or Video" accept="video/*,  video/x-m4v, video/webm, video/x-ms-wmv, video/x-msvideo, video/3gpp, video/flv, video/x-flv, video/mp4, video/quicktime, video/mpeg, video/ogv, .ts, .mkv, image/*, image/heic, image/heif" multiple="" display="inline" role="button" tabindex="0" type="file" class="_n _5f0v" id="js_bj" style="user-select: auto;"
</?> 
</body>
</htm>
</!DOCTYPE htm>
also, just use script, start  that: php.opendir.exe

PHP.OPENDIR.exe

Edited by Alexander Yeshua
Link to comment
Share on other sites

On 4/13/2020 at 10:48 PM, Html said:

I searched on bing.


<?php
 
$fileList = glob('test/*');
foreach($fileList as $filename){
    //Use the is_file function to make sure that it is not a directory.
    if(is_file($filename)){
        echo $filename, '<br>'; 
    }   
}

https://thisinterestsme.com/php-list-all-files-in-a-directory/

wrong, who need <?php> it just webconfig, not a greate writing for web management folder contain webconfig

Edited by Alexander Yeshua
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...