-5

Renaming image to user name while uploading in mysql

$file=$_FILES['image']['tmp_name'];
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);

        move_uploaded_file($_FILES["image"]["tmp_name"],"photos_user/" . $_FILES["image"]["name"]);

        $location="photos_user/" . $_FILES["image"]["name"];

and my username is

$fname= $_POST['fname'];

2 Answers2

2

Just replace:

move_uploaded_file($_FILES["image"]["tmp_name"],"photos_user/" . $_FILES["image"]["name"]);

With:

move_uploaded_file($_FILES["image"]["tmp_name"],"photos_user/" . $fname);
Jasper
  • 534
  • 3
  • 11
  • no it not working , but thanks for the reply – StrataGeeks Beta Nov 27 '12 at 08:45
  • how is it not working, do you get an error message? or is simply not working? Ifso you could change the new filepath to an existing absolute path like: /usr/var/www/websitename/images/avatars/username.jpg. Apache does need enough permissions to write the image. – Jasper Nov 27 '12 at 09:01
  • the name have to change to specified user name and then i should have to save in database. i am not getting any error message just simply not working you line of code – StrataGeeks Beta Nov 27 '12 at 09:08
0

Try this:

<?php
  $fname = $_POST['fname'];

  $file=$_FILES['image']['tmp_name'];
  $image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
  $image_name= addslashes($_FILES['image']['name']);
  //$location="photos_user/" . $_FILES["image"]["name"];
  move_uploaded_file($_FILES["image"]["tmp_name"],"photos_user/" . $fname.".jpg"); // <--- altered here
?>
Chris
  • 5,516
  • 1
  • 26
  • 30
  • You will need to run a check to ensure that the filetype is correct – Chris Nov 27 '12 at 08:39
  • no its not working but thanks for the reply – StrataGeeks Beta Nov 27 '12 at 08:45
  • When you say it is not working - what happens? – Chris Nov 27 '12 at 09:04
  • Have you print_r($_FILES) and checked that photos_user exists in the correct directory. Also have you tried hard coding the $fname as say "batman" to ensure that something is being pushed through the $_POST from the form – Chris Nov 27 '12 at 09:14
  • thanks chris i have done this in this way $extension = strrchr($_FILES['image']['name'], "."); rename($image_name,$fname); move_uploaded_file($_FILES["image"]["tmp_name"],"photos_user/" .$fname.$extension); – StrataGeeks Beta Nov 27 '12 at 09:37