Jump to content
Html

Image upload code specific part of where image displays?

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?

Share this post


Link to post
Share on other sites

Usually a tmp folder is set in php.ini, alongside  max file size and others.

Use phpinfo();

And find tmp folder location.

Share this post


Link to post
Share on other sites

Right, so the image is stored then in $temp, this value.

upload_tmp_dir    Directive

/tmp

 

So this is local value,

 

 

 

/tmp

And Master value

 

   

Share this post


Link to post
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.

 

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

You can look in folder for all files of a specific ext/s and then display them by looping through them getting file name then use that filename for src, when creating img tag.

Just google search "loop through image files in directory php"

Share this post


Link to post
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/

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

I don't know, I just watched a clip the other day by Eli the computer guy, and then posted here for some pointers.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...