How can I get the path to the file I am downloading?

im developing cms.

The clients of the table contain many fields, and one of them is the caption for the image.

When a user uploads a file (image), the file is saved in the shared folder.

The image header field extracts the final filename and stores it in a table.

The problem is when the user wants to update the information. If the user doesn't want to change the image when he clicks refresh, the image title field is empty, so the path to the image becomes null and now it just shows (no image).

here is what was trying to do:

HTML Form:

    <p>
        <label for="image_caption">Image Caption:</label>
    <input name="image_caption" id="image_caption" type="text" class="formbox" size="60"  disabled="disabled" value="<?php echo htmlentities($row['image_caption']); ?>" /> 
    </p>                                                                                     
    <p>
        <label for="image">Insert image:</label>
        <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
        <input type="file" name="image" id="foto" value="<?php echo htmlentities($row['image_caption']); ?>"/> 
    </p>
    <p>
        <?php 
        if($row['image_caption']!= ""){
        ?>
            <img src="../images/clientes/<?php echo $row['image_caption'];?>" width="194" height="145" title="Imagem cliente" class="floatright"/>
      <?php
        }else{
        ?>
            <img src="../images/sem_imagem.gif" width="194" height="145" title="Imagem não disponível" class="floatright"/><br />                                               
        <?php
        }
        ?>
  </p>        
    <p>
        <input type="submit" name="update" value="Update" />
        <input name="cliente_id" type="hidden" value="<?php echo $row['codigo']; ?>" />
    </p>

      

And now unload the PHP code (I just pasted the code I wanted to show to u):

    // This checks if the file input is empty, and if it is, insert the value from the table that was previously inserted

    if(empty($_FILES['image']['name'])){
$image_caption = $row['image_caption'];

}else{

$image = str_replace(' ', '_', $_FILES['image']['name']);   
// move the file to the upload folder and rename it
move_uploaded_file($_FILES['image']['tmp_name'],UPLOAD_DIR.$foto);  
$image_caption = $image;
}   

      

0


a source to share


3 answers


I'm still not sure I understand, but can you just pass the source file name as a hidden field? Then if nothing is loaded use that.

EDIT: Having just seen your comment, you already have the source file name, so you change your PHP to:

if(empty($_FILES['image']['name'])){
    $image_caption = $_POST['image_caption'];
}else{
    ....(etc)

      



should work, no?

And of course, if you are going to do something like this, then you have to do some sort of sanitization of the posted variables so that there is no nasty ...

+1


a source


Are you getting information from the database after submitting the form? I mean, does $ row ['nome_foto'] actually contain the previous path?

Edit: It would be better anyway to just not update the path field in the database unless a new path is specified.



Also, you need to add some form of validation to the upload to make sure it's an image. Now I can upload php script or virus to the server if I want.

0


a source


I'm not sure if I'm getting it right, but I'll give it a chance if you've filled out the update form with data from the database - then the image title field won't be empty even if the guy changes his name and everything else.

0


a source







All Articles