Jump to content

Recommended Posts

Ok, Hi,

 

My propblem is that I have a form which allows people to upload a image (gif,jpeg,jpg,bmp,png).

 

the image can be catched using:

$_FILES['photo1']['name']

 

I can upload the image and save the image name in my database, but the problem I have is I need to rename the image but keep the extention the same.

 

So my question summed up is:

 

How do I rename the image while keeping its extention and then save this to variable?

 

 

 

Please can someone help, my I have tried searching, but was not successfull.

Cheers

Sean

 

This is a very simple example.

 

$extension = strrchr($_FILES['photo1']['name'], ".");
$newname = "newname";
$newimage = $newname.$extension;
echo $newimage;

 

Caesar your code works but what if the name is mypic.john.jpg

 

:)

 

Ray

Hi, I have tried both, but did not manage to get it to work.

 

my full code is:

 

$user = $_SESSION['username'];

////INSERT ALL VARIABLES INTO DATABASE!

$connect = mysql_connect($host,$username,$password) or die("Error connecting to Database!". mysql_error());
mysql_select_db($database,$connect) or die("cannot select database!".mysql_error());


$check = mysql_query("select id from data where username='$user'") or die(mysql_error());
if (mysql_num_rows($check) > 0) {
     $a_record = mysql_fetch_array($check);
     $id = $a_record['id'];
}
//Creat image directory
mkdir("../../pages/".$id."/images");
$upload_dir = '../../pages/';  // You will need to set this. 
$upload_file = $upload_dir . $id ."/images/" . basename($id . "_" . $_FILES['photo1']['name']); 

$photo1url = $id . "_" . $_FILES['photo1']['name'];

//Get database cell - photo1url
mysql_select_db(********)
or die('Error, cannot select mysql database');

$query = "UPDATE properties SET photo1url = '$photo1url'". "WHERE id = '$id'";

mysql_query($query) or die('Error, query failed');

 

Thabove gets the image uploads it and saves the image name in the database, what I need to do is rename this. would somebody be able to rewrite this so it will do that.

 

I would be very, very greatful.

Cheers

Sean

What do you want to rename it to. It looks like you are renaming it already

 

$upload_file = $upload_dir . $id ."/images/" . basename($id . "_" . $_FILES['photo1']['name']);

 

id_filename.ext

 

Not sure what you want I guess.

 

Ray

by one I am guessing that would be the id of the current user.

 

<?php
$user = $_SESSION['username'];

////INSERT ALL VARIABLES INTO DATABASE!

$connect = mysql_connect($host,$username,$password) or die("Error connecting to Database!". mysql_error());
mysql_select_db($database,$connect) or die("cannot select database!".mysql_error());


$check = mysql_query("select id from data where username='$user'") or die(mysql_error());
if (mysql_num_rows($check) > 0) {
     $a_record = mysql_fetch_array($check);
     $id = $a_record['id'];
}
//Creat image directory
mkdir("../../pages/".$id."/images");
$upload_dir = '../../pages/';  // You will need to set this.
$ext = strrchr($_FILES['photo1']['name'], ".");
$n_name = $id;   // Set this to whatever you want the name to be
$newname = $n_name.$ext;
$upload_file = $upload_dir . $id ."/images/" . $new_name;
move_uploaded_file($_FILES['photo1']['tmp_name'], $upload_file);
// this will just insert the new name. You will have to point to the path when you output results from the db
$photo1url = $newname;

//Get database cell - photo1url
mysql_select_db(********)
or die('Error, cannot select mysql database');

$query = "UPDATE properties SET photo1url = '$photo1url'". "WHERE id = '$id'";

mysql_query($query) or die('Error, query failed');
?>

 

Ray

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.