Jump to content

upload a file and change the name


jasonhardwick

Recommended Posts

Hi, I'm trying to build an add/edit area for an image that a user will update so i have and "edit_profile_image.php" file that contains the image form and a "update_profile_image.php" that contains the code below. I also am trying to change the name of the file to the users ID but it dosent work... all i get is the echo "Sorry, there was a problem uploading your file." I think i am close but don't know what my next move is.

 

<?php

include "config.php";
$tbl_name="user";
$username=$_SESSION['username'];
$id=$_GET['id'];
$sql=( "UPDATE $tbl_name SET user_image = '$user_image' WHERE uname='$username'");
$rows=mysql_query($sql);

$user_image=$rows['user_image'];

function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 

$ext = findexts ($_FILES['user_image']['name']) ;

$new_name = $id.".";

//This assigns the subdirectory you want to save into... make sure it exists!
$target = "user_images/";
//This combines the directory, the random file name, and the extension
$target = $target . $new_name.$ext;
if(move_uploaded_file($_FILES['user_image']['tmp_name'], $target)) 
{
echo "The file has been uploaded as ".$new_name.$ext;
} 
else
{
echo "Sorry, there was a problem uploading your file.";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/106123-upload-a-file-and-change-the-name/
Share on other sites

ok i got my code to give me an ext but the filename is missing can anyone see why?

<?php

include "config.php";
$tbl_name="user";
$username=$_SESSION['username'];

$sql=( "UPDATE $tbl_name SET user_image = '$user_image' WHERE uname='$username'");
$rows = mysql_query($sql);

$id = $rows['id'];
$user_image = $rows['user_image'];

function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 

$ext = findexts ($_FILES['user_image']['name']) ;

$new_name = $id.".";

//This assigns the subdirectory you want to save into... make sure it exists!
$target = "user_images/";
//This combines the directory, the random file name, and the extension
$target = $target . $new_name.$ext;
echo $id;
if(move_uploaded_file($_FILES['user_image']['tmp_name'], $target)) 
{
echo "The file has been uploaded as ".$new_name.$ext;
} 
else
{
echo "Sorry, there was a problem uploading your file.";
}
?>

 

 

Change:

<?php
$rows = mysql_query($sql);

$id = $rows['id'];
$user_image = $rows['user_image'];
?>

To:

<?php
$rows = mysql_query($sql);
while($data = mysqli_fetch_assoc($rows)) {
$id = $data['id'];
$user_image = $data['user_image'];
}
?>

 

Even though you only have one result, I think you still have to use the while statment.  However, I have been wrong before, multiple, multiple times.

 

Also, this code assumes there is only one update made.  If there is more than one update made, for some odd reason, you might not get the information you want.

you don't have to use while for one record, but you should check to make sure your SQL doesn't error and that you get at least one record. any you shouldn't mix mysql_ and mysqli_ functions:

 

<?php
$rows = mysql_query($sql) or die(mysql_error());
if (mysql_numrows($rows) > 0) {
     $data = mysql_fetch_assoc($rows);
     $id = $data['id'];
     $user_image = $data['user_image'];
}
?>

heres my file upload function:

<?php

function uploadFile ($oldFile, $userFile, $imgDir) {
//extensions array
$validExt = array ('.jpg', '.jpeg', '.png', '.gif');
//get the file ext
$ext = strrchr($userFile, ".");
$ext = strtolower($ext);
//check the file ext against the array
if(in_array($ext, $validExt)) {
	//needle is in haystack
	//process upload
	//rename the file
	$newName = md5(rand() * time()) . $ext;
	//new file dest
	$destFile = $imgDir . $newName;
	//move the file to tmpDir
	$result = move_uploaded_file($oldFile, $destFile);
	if($result == false) {
		//there was an error uploading
		return false;
	}else{
		//file was uploaded
		chmod($destFile, 0777);
		return $destFile;
	}
}else{
	//needle is not in haystack. exit
	return false;
}
}

?>

 

this takes a file from a form and uploads it to the dir. of your choice.

the function also creates a random filename, still using the pre-existing ext.

so gives something like:

jh4g5hjg34jh5bjh34bhjb34jh3poi345g345.doc

the $oldFile is the name of the tmp file where your server stores files to upload.

$userFile is the name of the file that the user selected

$imgDir is the location you want to upload the file to in relation to the location of where this function is used.

 

 

so, you use it in your web root and you want your file to be upload to root/uploads/word/user/

then you would us:

$dir = "uploads/word/user/";

 

and call the function like so:

<?php

$tmpFile = $_POST['file']['tmp_name'];
$userFile = $_POST['file']['name'];
$dir = "uploads/word/user/";

$upload = uploadFile($tmpFile, $userFile, $dir);

if ($upload == false) {
echo "There was a problem uploading the file.";
}else{
echo "The file was uploaded and renamed to : ".$upload.".";
}
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.