Jump to content

Recommended Posts

My 1st try practice uploading file to the server and it was successful.

 

Now I wonder how can I set a default file name to the file that will be uploaded and overwrite the existing one.

 

I have tried experimenting on the code but I can't get it to work.

 

 

Here's my simple code..

 

edit_logo.php

 

<?php

<form enctype="multipart/form-data" method="post"  action="uploaded_logo.php">

<input type="hidden" name="MAX_FILE_SIZE" value="100000" />

<tr>
<td>Choose a Logo to Upload: </td>
    <td><input name="uploaded_file" type="file" /></td>
</tr>

<tr>
<td> </td>
    <td><input type="submit" value="Upload File" name="submit>"</td>
</tr>




</form>

?>

 

 

uploaded_logo.php

<?php

if($_FILES['uploaded_file']["type"] == "image/gif")
{

$target_path = "logo/";

$target_path = $target_path. basename($_FILES['uploaded_file']['name']);



if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path))
{
echo "<span class=\"error_validation\">The Logo has been successfully changed!<br></span>";
echo "<span class=\"error_validation\">Upload Logo Again? <a href=\"edit_logo.php\">Click Here</a><br></span>";
}
else
{
echo "<span class=\"error_validation\">There was an error uploading the logo. Pls. try again.<br></span>";
echo "<span class=\"error_validation\">Upload Logo Again? <a href=\"edit_logo.php\">Click Here</a><br></span>";
}

}
else
{
echo "<span class=\"error_validation\">Invalid file format. We are only accepting image file. Pls. try again.<br></span>";
echo "<span class=\"error_validation\">Upload Logo Again? <a href=\"edit_logo.php\">Click Here</a><br></span>";
}


?>

 

 

So what the code suppose to do is.. If I upload an image file as the new logo it should have the default logo name "my_logo.gif", then it will overwrite the existing one...

 

Anyone?

$target_path = $target_path. basename($_FILES['uploaded_file']['name']);

Change this to have the name you desire.

 

Thank you it's working now.

 

Though how would I able to get the extension of that file? instead of typing it like the code below "Logo.gif"

 

<?php

 

$target_path = $target_path. basename('Logo.gif');

 

?>

 

 

?

pathinfo works, but is quite a bit slower than using arrays, and even slower than string functions.

 

I don't think it would make a huge difference beyond the most efficiency-demanding applications though, so use whatever you find cleanest. This post is just for academic purposes.

Ahh that's good to know. I might try the array method in future :)

 

pathinfo works, but is quite a bit slower than using arrays, and even slower than string functions.

 

I don't think it would make a huge difference beyond the most efficiency-demanding applications though, so use whatever you find cleanest. This post is just for academic purposes.

I worded that badly.

 

In terms of time to execute

 

pathinfo > explode/array > substr/strings

 

So, string functions are the most efficient way to get the result. Quite accurate too, unless you're dealing with .tar.gz, etc. Whether 'tar' is actually an extension in this case is debated though.

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.