Menu
Hi, i am using this code to currently upload a file
but how can i rename the item, with keeping the extension the same.
[code=php]
<?PHP
$file_dir = “examples/”;
foreach($_FILES as $file_name => $file_array) {
echo “Path: “.$file_array[‘tmp_name’].”<br>n”;
echo “Name: “.$file_array[‘name’].”<br>n”;
echo “Type: “.$file_array[‘type’].”<br>n”;
echo “Size: “.$file_array[‘size’].”<br>n”;
if (is_uploaded_file($file_array[‘tmp_name’])) {
move_uploaded_file($file_array[‘tmp_name’],
“$file_dir/$file_array[name]”) or die (“Couldn’t copy”);
echo “file was moved!<br><br>”;
}
}
?>
Many Thanks
Adam
[code=php]
$file = $_FILE['upload']['tmp_name'];
$ext = explode('/', mime_content_type($file));
move_uploaded_file($file, 'images/uploaded/' . $new_name . $ext[1]);
[/code]
[code=php]
if (is_uploaded_file($file_array['tmp_name'])) {
$file = $_FILE['upload']['tmp_name'];
$ext = explode('/', mime_content_type($file));
move_uploaded_file($file, 'example/' . $new . $ext[1]);
echo "file was moved!<br><br>";
}
}
Many Thanks Adam
[/code]
[code=php]
$ext = explode('.', $file);
[/code]
[code=php]
<?PHP
$file_dir = "examples/";
foreach($_FILES as $file_name => $file_array) {
echo "Path: ".$file_array['tmp_name']."<br>n";
echo "Name: ".$file_array['name']."<br>n";
echo "Type: ".$file_array['type']."<br>n";
echo "Size: ".$file_array['size']."<br>n";
if (is_uploaded_file($file_array['tmp_name'])) {
$file = $_FILE['upload']['tmp_name'];
$ext = explode('.', $file);
$new = newname //is this where the new name goes
move_uploaded_file($file, 'example/' . $new . $ext);
echo "file was moved!<br><br>";
}
}
?>
[/code]
[code=php]
//$filename = "blahblahblah.some.thing.txt";
//step 1: Split the filename into bits, using the period as the delimiter (or separator) and place those bits into an array
$nameparts = explode(".", $filename);
/* In this example, the array, $nameparts, now has the following:
$nameparts[0] has 'blahblahblah'
$nameparts[1] has 'some'
$nameparts[2] has 'thing'
$nameparts[3] has 'txt'
*/
// Question: How do we tell it that we want the last element, $nameparts[3] ?
// step 2: Get the number of elements:
$num_elements = sizeof($nameparts);
// you can also say: $num_elements = count($nameparts)...PHP is rich with alternative ways of doing things.
/* In this example, you can see that the number of elements will be four. You can also see, very clearly, that the last element, the fourth one, has an index of 3. It has to have an index which is ONE less than the maximum because the array index starts from zero.
In other words, to get the file extension, we need to give the array an index of ONE LESS THAN the MAXIMUM number of elements.
*/
// Step 3: Index of Last element:
$index_of_last = $num_elements - 1;
// Step 4: Get the Extension
$extension = $nameparts[$index_of_last];
[/code]
[code=php]
<?PHP
$file_dir = "examples/";
//This breaks it up into its seperate parts
foreach($_FILES as $file_name => $file_array) {
echo "Path: ".$file_array['tmp_name']."<br>n";
echo "Name: ".$file_array['name']."<br>n";
echo "Type: ".$file_array['type']."<br>n";
echo "Size: ".$file_array['size']."<br>n";
//if the file is allowed
if (is_uploaded_file($file_array['tmp_name'])) {
$file = $_FILE['upload']['tmp_name'];
//breaks the extensions up into parts
$nameparts = explode(".", $file);
//find the number of extensions
$num_elements = sizeof($nameparts);
//this locates the last element by find the total and takin away one
$index_of_last = $num_elements - 1;
//this is the new extension
$ext = $nameparts[$index_of_last];
//the new name
$new = newname;
move_uploaded_file($file, 'examples/' . $new . $ext);
echo "file was moved! $ext <br><br>";
}
}
?>
[/code]
[code=php]
//the new name
$new = newname;
[/code]
[code=php]
ove_uploaded_file($file, 'examples/' . $new . $ext);
[/code]
[code=php]
<?php
$new_name = "example/test"; // The script adds the right extension
foreach($_FILES as $file_name => $file_array) {
echo "Path: ".$file_array['tmp_name']."<br>n";
echo "Name: ".$file_array['name']."<br>n";
echo "Type: ".$file_array['type']."<br>n";
echo "Size: ".$file_array['size']."<br>n";
$tmp_name = $file_array['tmp_name'];
$filename = $file_array['name'];
if(is_uploaded_file($tmp_name)) {
$ext = explode(".", $filename);
$ext = $ext[count($ext)-1];
move_uploaded_file($tmp_name, $new_name.".".$ext) or die("Couldn't copy");
echo "file was moved!<br><br>";
}
}
?>
[/code]
[code=php]
<?php
$new_name = "examples/"; // The script adds the right extension
//just to display the outcome
foreach($_FILES as $file_name => $file_array) {
echo "Path: ".$file_array['tmp_name']."<br>n";
echo "Name: ".$file_array['name']."<br>n";
echo "Type: ".$file_array['type']."<br>n";
echo "Size: ".$file_array['size']."<br>n";
$tmp_name = $file_array['tmp_name'];
$filename = $file_array['name'];
if(is_uploaded_file($tmp_name)) {
$ext = explode("me", $filename);
//this should break down the extenions
$ext = $ext[count($ext)-1];
move_uploaded_file($tmp_name, $new_name."me".$ext) or die("Couldn't copy");
echo "file was moved!<br><br>";
}
}
?>
[/code]
[code=php]
<?php
//this is the directory it will go into
$dir = "examples/";
//this is the name of the file
$new_name = "$_POST[newname]";
//this is what to display if it is all correct
foreach($_FILES as $file_name => $file_array) {
echo "Path: ".$file_array['tmp_name']."<br>n";
echo "Name: ".$file_array['name']."<br>n";
echo "Type: ".$file_array['type']."<br>n";
echo "Size: ".$file_array['size']."<br>n";
//this sets the temp name and the name as a variable
$tmp_name = $file_array['tmp_name'];
$filename = $file_array['name'];
//this uploads the file
if(is_uploaded_file($tmp_name)) {
//this code seperates the uploaded file so i can retreive the extension
$ext = explode(".", $filename);
$ext = $ext[count($ext)-1];
//moves from the temp directory to the loacation i want and changes the name
move_uploaded_file($tmp_name, $dir.$new_name.".".$ext) or die("Couldn't copy");
echo "file was moved!<br><br>";
}
}
?>
[/code]
0.1.9 — BETA 4.29