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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with str_replace(" ", "_", $file_image); 3

Status
Not open for further replies.

purcion

Programmer
Feb 14, 2005
32
AU
hi ,
I have a series of pages 1 has input forms that send variables to a page that uploads and submits the files and text to mysql . It works fine until someone tries to upload a file with white space in the name the problem im having is
in my submit page I cant place the str_replace function would someone be so kind as to help me to place this properly.

cheer

code below without db connection stuff
can someone plaese add this to it
str_replace(" ", "_", $file_image);
===============================================

<?
//grabs the variables

$title = $_POST["title"]; //brief title blurb
$image = $_POST["image"]; // anchor tag for image
$name = $_POST["name"]; // application name
$description = $_POST["description"]; // long description
$file_up = $HTTP_POST_FILES['file']['name']; // file app upload
#$file_source =$HTTP_POST_FILES['file_source']['name']; // file source upload
$file_image = $HTTP_POST_FILES['file_image']['name']; // screengrab image upload

$file_size = round($HTTP_POST_FILES['file']['size'] / 1024)."kbs"; // app file size kbs

#$source_size = round($HTTP_POST_FILES['file_source']['size'] / 1024)."kbs"; // source file size kbs

$kb = 1024; // Kilobyte
$mb = 1024 * $kb; // Megabyte
$gb = 1024 * $mb; // Gigabyte
$tb = 1024 * $gb; // Terabyte
if($file_size < $kb) {
$file_size." B";
}
else if($file_size < $mb) {
round($file_size/$kb,2)." KB";
}
else if($file_size < $gb) {
round($file_size/$mb,2)." MB";
}
else if($file_size < $tb) {
round($file_size/$gb,2)." GB";
}
else {
round($file_size/$tb,2)." TB";
}
$date = date("j F Y");
$addnews =MYSQL_QUERY("INSERT INTO tblZ_test (id,title,date,image,name,description,file_up,file_size,file_image)". "VALUES
('NULL', '$title', '$date', '$image','$name','$description','$file_up','$file_size','$file_image')");
//success...
{

//File has passed all validation, copy it to the final destination and remove the temporary file:
//copy application and upload
copy($HTTP_POST_FILES['file']['tmp_name'],"images/".$HTTP_POST_FILES['file']['name']);
unlink($HTTP_POST_FILES['file']['tmp_name']);
//copy source and upload
// copy($HTTP_POST_FILES['file_source']['tmp_name'],"images/".$HTTP_POST_FILES['file_source']['name']);
// unlink($HTTP_POST_FILES['file_source']['tmp_name']);
//copy image and upload
copy($HTTP_POST_FILES['file_image']['tmp_name'],"images/".$HTTP_POST_FILES['file_image']['name']);
unlink($HTTP_POST_FILES['file_image']['tmp_name']);
// print "File has been successfully uploaded!";
//exit;
}
//echo("Description Added!");
?>

=====================================================
thanks
 
Since you already have the name locally thru

Code:
$file_image = $HTTP_POST_FILES['file_image']['name'];

why not do

Code:
$file_image = $HTTP_POST_FILES['file_image']['name'];
$file_image = str_replace(" ","_",$file_image);
... copy($HTTP_POST_FILES['file']['tmp_name'],"images/$file_image");

Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
That is halfway there (thanks) that does change the final file name
which is great because it strips white space from the file

but the problem that it has is that when the file is uploaded it uploads the file to a size of 0 bytes
I use a getimagesize() on another page to access this file and it throws a cant seek file error obviously because the file is 0 bytes it contains no data. thanks again for your help hopefully someone will know how to make this work.
i ll add the code again with Bastiens suggested changes
if there is something missing to make the bytes upload please add
Thanks again
===================================================
<?
//grabs the variables

$title = $_POST["title"]; //brief title blurb
$image = $_POST["image"]; // anchor tag for image
$name = $_POST["name"]; // application name
$description = $_POST["description"]; // long description
$file_up = $HTTP_POST_FILES['file']['name']; // file app upload
#$file_source =$HTTP_POST_FILES['file_source']['name']; // file source upload
$file_image = $HTTP_POST_FILES['file_image']['name']; // screengrab image upload
$file_image = str_replace(" ","_",$file_image);


$file_size = round($HTTP_POST_FILES['file']['size'] / 1024)."kbs"; // app file size kbs

#$source_size = round($HTTP_POST_FILES['file_source']['size'] / 1024)."kbs"; // source file size kbs

$kb = 1024; // Kilobyte
$mb = 1024 * $kb; // Megabyte
$gb = 1024 * $mb; // Gigabyte
$tb = 1024 * $gb; // Terabyte
if($file_size < $kb) {
$file_size." B";
}
else if($file_size < $mb) {
round($file_size/$kb,2)." KB";
}
else if($file_size < $gb) {
round($file_size/$mb,2)." MB";
}
else if($file_size < $tb) {
round($file_size/$gb,2)." GB";
}
else {
round($file_size/$tb,2)." TB";
}
$date = date("j F Y");
$addnews =MYSQL_QUERY("INSERT INTO tblZ_test (id,title,date,image,name,description,file_up,file_size,file_image)". "VALUES
('NULL', '$title', '$date', '$image','$name','$description','$file_up','$file_size','$file_image')");
//success...


//File has passed all validation, copy it to the final destination and remove the temporary file:
//copy application and upload
copy($HTTP_POST_FILES['file']['tmp_name'],"images/".$HTTP_POST_FILES['file']['name']);
unlink($HTTP_POST_FILES['file']['tmp_name']);
//copy source and upload
// copy($HTTP_POST_FILES['file_source']['tmp_name'],"images/".$HTTP_POST_FILES['file_source']['name']);
// unlink($HTTP_POST_FILES['file_source']['tmp_name']);
//copy image and upload
copy($HTTP_POST_FILES['file_image']['tmp_name'],"images/$file_image");
unlink($HTTP_POST_FILES['file_image']['tmp_name']);
// print "File has been successfully uploaded!";
//exit;

//echo("Description Added!");
?>
 
Oh man my fault sorry Bastein
I was uploading a file with zero bytes haha
hey thanks for your help really apreciate it
Ill now go off and stop burning with embarassment ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top