Jump to content

[SOLVED] Renaming a File


monkeytooth

Recommended Posts

I am setting up something so my users can upload files. Which with that I am able to do, however I want to rename the files after they are uploaded. Which is where I am stuck. Can anyone help me on this?

 

I can get the existing file name using this:

 

$_FILES['file']['name']

 

what I am doing is uploading to a community folder so to speak, and keeping track of who's file is who's via a database. I just want all the files to be in one unknown spot on the server with new names on the files so they can only be accessed via my scripts (for the most part)..

Link to comment
https://forums.phpfreaks.com/topic/132199-solved-renaming-a-file/
Share on other sites

This is what I am doing now..

 

if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
print "Your file has been uploaded successfully! Yay!<br />" . $_FILES['file']['name'] . "";
} else {
print "Incorrect file extension!";
}

 

How would I flop that in? Sorry to sound off about things.. last time I did file uploads with php, the whole move_ thing wasnt around so im a bit off relearning as i go with this one...

Use

if(is_uploaded_file($_FILES['file']['tmp_name']))
{
    $new_filename = (microtime(true) . getmypid()) . $_FILES['file']['name'];

    move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$new_filename);
    print "Your file has been uploaded successfully! Yay!<br />" . $new_filename;
}
else
{
    print "Incorrect file extension!";
}

ive swapped it, but now im getting an unexpected $end.. not sure from what all the brackets seem to be in place my guess something with the

$new_filename = (microtime(true) . getmypid()) . $_FILES['file']['name']; 

but i dunno, something to me just seems off with that

<?php
$uploaddir = "fileupz"; 
$allowed_ext = "doc, pdf, png, jpg"; 
$max_size = "460800"; // 450kb
$max_height = ""; //if intent on uploading images hxw..
$max_width = ""; 

$extension = pathinfo($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
if ($allowed_paths[$i] == "$extension") {
$ok = "1";
}
}

if ($ok == "1") {
if($_FILES['file']['size'] > $max_size) {
print "File size is too big!";
exit;
} elseif($_FILES['file']['size'] <= "0") {
print "File size is too Small 0kb!";
exit;
} else {
}

if ($max_width && $max_height) {
list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']);
if($width > $max_width || $height > $max_height)
{
print "File height and/or width are too big!";
exit;
}
}

if(is_uploaded_file($_FILES['file']['tmp_name'])){
    $$new_filename = microtime(true) . getmypid() . $_FILES['file']['name'];

    move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$new_filename);
    print "Your file has been uploaded successfully! Yay!<br />" . $new_filename;
} else {
    print "Incorrect file extension!";
}
?>

You do not have a closing brace for this if:

if ($ok == "1") {

 

However looking at your script. This portion of code

$allowed_ext = "doc, pdf, png, jpg"; 
...
$extension = pathinfo($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
if ($allowed_paths[$i] == "$extension") {
$ok = "1";
}
}

if ($ok == "1") {

can be changed to

$allowed_ext = array("doc", "pdf", "png", "jpg");
...
$img = pathinfo($_FILES['file']['name']);
if (in_array($img['extension'], $allowed_ext))

Which is much cleaner.

 

Everything put together:

<?php
$uploaddir = "fileupz";
$allowed_ext = array("doc", "pdf", "png", "jpg");
$max_size = "460800"; // 450kb
$max_height = ""; //if intent on uploading images hxw..
$max_width = "";

$img = pathinfo($_FILES['file']['name']);
if (in_array($img['extension'], $allowed_ext))
{
    if($_FILES['file']['size'] > $max_size)
    {
        print "File size is too big!";
        exit;
    }
    elseif($_FILES['file']['size'] <= "0")
    {
        print "File size is too Small 0kb!";
        exit;
    }

    if ($max_width && $max_height)
    {
        list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']);
        if($width > $max_width || $height > $max_height)
        {
            print "File height and/or width are too big!";
            exit;
        }
    }

    if(is_uploaded_file($_FILES['file']['tmp_name']))
    {
        $new_filename = microtime(true) . getmypid() . $_FILES['file']['name'];

        move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$new_filename);
        print "Your file has been uploaded successfully! Yay!<br />" . $new_filename;
    }
    else
    {
        print "Incorrect file extension!";
    }
}

?>

well that seems to have worked wonders, thanks for the help with that.. now all i gotta do is figure out to check to see if file exists (though I doubt the possibility exists unless i go a different route of rename.. may not though as i doubt there will ever be a chance in hell of the same file name ever with this concept..

 

then throw in the DB stuff to store what I want off of this, and ill be good I think.. Thanks again..

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.