Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using substr in an array problem..

Status
Not open for further replies.

sd0t1

IS-IT--Management
Mar 14, 2007
131
US
I have a form that uploads 3 .jpg's to a folder and lists the names of the .jpg's in a database table. It works fine unless I try to strip off the ".jpg" from the end of the image name.
This is what it does. example. if image name is "imgname.jpg" and the database fields are image1,image2,image3.

image1 image2 image3
. j p


should be:

image1 image2 image3
imgname imgname imgname


instead of giving me the name of each image with out the ".jpg"
Any help is appreciated.
thanks in advance..

here is my code

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && ($_POST['photo_desc'] !="")) {
$file_dir = "images/";
for($i=0; $i<3;$i++){
//test if upload was complete
if(isset($_FILES['upfile']['error'][$i]) && $_FILES['upfile']['error'][$i]==0 && $_FILES['upfile']['name'][$i]!= ""){
$newfile[$i] = $file_dir.$_FILES['upfile']['name'][$i];
move_uploaded_file($_FILES['upfile']['tmp_name'][$i], $newfile[$i]);
$name = substr($_FILES['upfile']['name'][$i],-4);
$j++;
if (isset($j)&&$j>0) $message = "Your file(s) has been uploaded.<br><br>Choose 3 files to upload...<br>";
}else{
$newfile[$i] = 'File not uploaded';
}
}
$insertSQL = sprintf("
INSERT
INTO
photo_table
(
photo_id,
photo_client_id,
photo_category,
photo_name,
photo_keyword,
photo_desc,
photo_thumb_name,
photo_4x6_name,
photo_6x9_name
)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['photo_id'], "int"),
GetSQLValueString($_POST['photo_client_id'], "int"),
GetSQLValueString($_POST['photo_category'], "int"),
GetSQLValueString($_POST['photo_name'], "text"),
GetSQLValueString($_POST['photo_keyword'], "text"),
GetSQLValueString($_POST['photo_desc'], "text"),
GetSQLValueString($name[0], "text"),
GetSQLValueString($name[1], "text"),
GetSQLValueString($name[2], "text")
);
mysql_select_db($database_database, $database);
mysql_query($insertSQL, $database) or die(mysql_error());
 
It's in the part where I upload the image, then it takes the name strips off the last 4 and inserts it into the database. This is the piece of code i'm referring to.

$name = substr($_FILES['upfile']['name'][$i],-4);

I want to take the array called $name and take off the .jpg. I thought by adding the substr($name, -4) it would take off the last 4 characters (.jpg).
 
Sorry, I missed it.

You're trying to strip off the last for characters invoking substr() using a negative start value? Read the PHP online manual entry for substr{}, paying particular attention to the example code.



Want the best answers? Ask the best questions! TANSTAAFL!
 
have a look also at basename()
and pathinfo()
and of course you could also use explode()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top