Upload Profile Image not saved

Asked

Viewed 44 times

0

Hello,

I have a user register where it is possible to insert a profile photo. Every time I try to insert an image, warning messages are displayed. I have tried some internet solutions but unfortunately I could not solve my problem.

Messages:

"Warning: move_uploaded_file(): The Second argument to copy() Function cannot be a directory in"

Warning: move_uploaded_file(): Unable to move

BD:

Name | Type

Image| blob

HTML:

<div class="form-group">
<label class="col-md-4 control-label" for="imagem">Imagem de perfil</label>
<div class="col-md-4">
<input type="file" name="imagem" class="btn btn-success"  accept="image/*" > 
</div>
</div>

PHP:

$tmpName = $_FILES['imagem']['tmp_name']; 
$imagem= $_FILES['imagem']['name']; 
$arqError = $_FILES['imagem']['error'];

if ($arqError == 0) {
        $pasta = './uploads/';
        $upload = move_uploaded_file($tmpName, $pasta);
    }

PHP to display the image:

<img src="uploads/<?php=$_FILES['imagem']['name'];?>">
  • The PHP Notice is stating that the second argument cannot be a directory. It is very likely that you are not receiving the name of the file to be saved, but only the directory. Try to see the content of this argument used the function var_dump($argumento);

  • @Carlosandrade thank you, I will try and in case would var_dump($image)

1 answer

1


As I mentioned in the comment above, the variable $pasta is only receiving the directory and not the file name. Change your code to the following and see if it works:

$tmpName = $_FILES['imagem']['tmp_name']; 
$imagem= $_FILES['imagem']['name']; 
$arqError = $_FILES['imagem']['error'];

if ($arqError == 0) {
        $pasta = './uploads/';
        $upload = move_uploaded_file($tmpName, $pasta.'imagem.jpg');
}
  • It worked, but before I vote on your answer, could you do me a favor? The image is being saved but now it is not being displayed, would know me tell of the pq?

  • Try the following: <img src="<?php echo $pasta.'imagem.jpg'; ?>">

  • First check whether $pasta is actually receiving the uploads directory. Note: replace imagem.jpg for a variable with value of your choice, do not leave so, otherwise every new image you upload will replace the existing one.

  • Thanks for your help, I’ve already made the adjustments to your answer according to your suggestion. But I show the profile photo on another page, different from the registration and I believe that’s why your suggestion of how to display the photos did not work

  • I understand. You should then save the file name in the database (if this is the case) at the time the image was saved. So take this data and replace it in the example. It would look something like this: <img src="<?php echo './uploads/'.$img_perfil; ?>">

  • I’m sorry I didn’t understand very well, I save in the comic $imagem. And my code went like this <img src="<?php echo './uploads/'.$imagem; ?>">

  • Exactly. In the Upload Script, after sending you should save the file name information in your register and then display it. For example: you will record imagem.jpg in a field of the user registration table, you already have these fields: ID, NAME, LOGIN, etc... Then add an IMG_PERFIL field and save in this field the name of the Upload file. On the page where the profile image will be displayed, read this field and store it in a variable like the one above $imagem.

  • Thanks, I’ll try thanks for the force!!!

  • @Malfus if it goes right post the result.

  • It didn’t work, I did it this way : <?php&#xA;$sql = mysql_query("SELECT imagem FROM usuarios");&#xA;$foto = mysql_fetch_object($sql);&#xA;?>&#xA;<img src="<?php echo './uploads/'.$foto; ?>">

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.