PHP upload files and give a custom name
I am using this function to upload my files:
if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = $_FILES['Artwork']['name'];
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}
How do I change $imageName = $_FILES['Artwork']['name']; with a custom name, but mantaining the file extension in the name, so for example: myCustomName.jpg?
Thanks!
php upload
add a comment |
I am using this function to upload my files:
if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = $_FILES['Artwork']['name'];
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}
How do I change $imageName = $_FILES['Artwork']['name']; with a custom name, but mantaining the file extension in the name, so for example: myCustomName.jpg?
Thanks!
php upload
1
Don't use the['type']field. That's user-provided data and is trivial to forge. use something server-side, such as FileInfo, to get a mime-type. Still not 100%, but much better than the 0% reliability of the['type']field.
– Marc B
Aug 23 '11 at 14:31
add a comment |
I am using this function to upload my files:
if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = $_FILES['Artwork']['name'];
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}
How do I change $imageName = $_FILES['Artwork']['name']; with a custom name, but mantaining the file extension in the name, so for example: myCustomName.jpg?
Thanks!
php upload
I am using this function to upload my files:
if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = $_FILES['Artwork']['name'];
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}
How do I change $imageName = $_FILES['Artwork']['name']; with a custom name, but mantaining the file extension in the name, so for example: myCustomName.jpg?
Thanks!
php upload
php upload
asked Aug 23 '11 at 13:57
DiegoP.DiegoP.
18.4k2779100
18.4k2779100
1
Don't use the['type']field. That's user-provided data and is trivial to forge. use something server-side, such as FileInfo, to get a mime-type. Still not 100%, but much better than the 0% reliability of the['type']field.
– Marc B
Aug 23 '11 at 14:31
add a comment |
1
Don't use the['type']field. That's user-provided data and is trivial to forge. use something server-side, such as FileInfo, to get a mime-type. Still not 100%, but much better than the 0% reliability of the['type']field.
– Marc B
Aug 23 '11 at 14:31
1
1
Don't use the
['type'] field. That's user-provided data and is trivial to forge. use something server-side, such as FileInfo, to get a mime-type. Still not 100%, but much better than the 0% reliability of the ['type'] field.– Marc B
Aug 23 '11 at 14:31
Don't use the
['type'] field. That's user-provided data and is trivial to forge. use something server-side, such as FileInfo, to get a mime-type. Still not 100%, but much better than the 0% reliability of the ['type'] field.– Marc B
Aug 23 '11 at 14:31
add a comment |
3 Answers
3
active
oldest
votes
The only line you need modified in your code is:
$imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
Where 'CustomName.' is the new name you want for the image.
pathinfo if the PHP function to handle the operations with paths and files names.
You whole code would be:
if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}
1
+1 to it. It is recommended the use ofpathinfofunction.
– diosney
Aug 23 '11 at 14:13
add a comment |
$ext = last(explode('.', $_FILES['Artwork']['name']));
$custom_name = 'something';
$imageName = $custom_name.'.'.$ext;
add a comment |
I think you're making it too complicated. It's as simple as splitting the filename by the dot and using the last element:
$parts = explode('.', $_FILES['Artwork']['name']);
$newname = "myCustomName" . (size($parts) > 1 ? '.' . last($parts) : '')
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7162157%2fphp-upload-files-and-give-a-custom-name%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The only line you need modified in your code is:
$imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
Where 'CustomName.' is the new name you want for the image.
pathinfo if the PHP function to handle the operations with paths and files names.
You whole code would be:
if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}
1
+1 to it. It is recommended the use ofpathinfofunction.
– diosney
Aug 23 '11 at 14:13
add a comment |
The only line you need modified in your code is:
$imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
Where 'CustomName.' is the new name you want for the image.
pathinfo if the PHP function to handle the operations with paths and files names.
You whole code would be:
if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}
1
+1 to it. It is recommended the use ofpathinfofunction.
– diosney
Aug 23 '11 at 14:13
add a comment |
The only line you need modified in your code is:
$imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
Where 'CustomName.' is the new name you want for the image.
pathinfo if the PHP function to handle the operations with paths and files names.
You whole code would be:
if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}
The only line you need modified in your code is:
$imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
Where 'CustomName.' is the new name you want for the image.
pathinfo if the PHP function to handle the operations with paths and files names.
You whole code would be:
if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}
edited Aug 23 '11 at 14:13
answered Aug 23 '11 at 14:04
leticialeticia
1,88652435
1,88652435
1
+1 to it. It is recommended the use ofpathinfofunction.
– diosney
Aug 23 '11 at 14:13
add a comment |
1
+1 to it. It is recommended the use ofpathinfofunction.
– diosney
Aug 23 '11 at 14:13
1
1
+1 to it. It is recommended the use of
pathinfo function.– diosney
Aug 23 '11 at 14:13
+1 to it. It is recommended the use of
pathinfo function.– diosney
Aug 23 '11 at 14:13
add a comment |
$ext = last(explode('.', $_FILES['Artwork']['name']));
$custom_name = 'something';
$imageName = $custom_name.'.'.$ext;
add a comment |
$ext = last(explode('.', $_FILES['Artwork']['name']));
$custom_name = 'something';
$imageName = $custom_name.'.'.$ext;
add a comment |
$ext = last(explode('.', $_FILES['Artwork']['name']));
$custom_name = 'something';
$imageName = $custom_name.'.'.$ext;
$ext = last(explode('.', $_FILES['Artwork']['name']));
$custom_name = 'something';
$imageName = $custom_name.'.'.$ext;
answered Aug 23 '11 at 14:00
yodayoda
6,844155888
6,844155888
add a comment |
add a comment |
I think you're making it too complicated. It's as simple as splitting the filename by the dot and using the last element:
$parts = explode('.', $_FILES['Artwork']['name']);
$newname = "myCustomName" . (size($parts) > 1 ? '.' . last($parts) : '')
add a comment |
I think you're making it too complicated. It's as simple as splitting the filename by the dot and using the last element:
$parts = explode('.', $_FILES['Artwork']['name']);
$newname = "myCustomName" . (size($parts) > 1 ? '.' . last($parts) : '')
add a comment |
I think you're making it too complicated. It's as simple as splitting the filename by the dot and using the last element:
$parts = explode('.', $_FILES['Artwork']['name']);
$newname = "myCustomName" . (size($parts) > 1 ? '.' . last($parts) : '')
I think you're making it too complicated. It's as simple as splitting the filename by the dot and using the last element:
$parts = explode('.', $_FILES['Artwork']['name']);
$newname = "myCustomName" . (size($parts) > 1 ? '.' . last($parts) : '')
answered Aug 23 '11 at 14:01
Aleks GAleks G
43.2k18126195
43.2k18126195
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7162157%2fphp-upload-files-and-give-a-custom-name%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Don't use the
['type']field. That's user-provided data and is trivial to forge. use something server-side, such as FileInfo, to get a mime-type. Still not 100%, but much better than the 0% reliability of the['type']field.– Marc B
Aug 23 '11 at 14:31