Gandalf
November 28, 2019, 10:15am
1
I have a script that uploads a document and I want it to give me the URI of the uploaded doc.
The upload script is at http://www.domain.com/folder/admin/upload.php
and the uploaded doc is at http://www.domain.com/folder/newsletters/news1.pdf
So far I have
echo '<p>Newsletter URL: <strong>', $_SERVER['REQUEST_SCHEME'], '://', $_SERVER['SERVER_NAME'], $dir, $file, '</strong></p>', "\n";
but that is missing part of the path ie the /folder/ bit. Is there any way to get that part of the path without hardcoding it?
Perhaps use this:
<?PHP
// https://www.php.net/manual/en/function.parse-url.php
$arrayOfUrlParts = parse_url($uploadedFile);
echo '<pre>'; print_r($arrayOfUrlParts); echo '</pre>';
Gandalf
November 28, 2019, 11:24am
3
John_Betong:
$uploadedFile
Yes, I looked at parse_url but that rather assumes I have $uploaded file in the first place.
I try the fire was passed to upload.php using get or post. The file path could be passed to parse_url(…);
Without knowing more information it is not easy to suggest solutions.
Gandalf
November 28, 2019, 12:43pm
5
Sure. What do you need to know? This is the part of the code that saves the uploaded file:
$dir = '/newsletter/';
$destdir = '..'.$dir;
if (!is_dir($destdir))
mkdir($destdir);
$file = 'news1.pdf';
$destpath = $destdir.$file;
$tmp_name = $_FILES['uppfile']['tmp_name'];
if (is_uploaded_file($tmp_name) && move_uploaded_file($tmp_name, $destpath)) {
echo '<p>File ', $filename, ' has been uploaded successfully as <strong>', $file, '</strong>.</p>', "\n";
echo '<p>Newsletter URL: <strong>', $_SERVER['REQUEST_SCHEME'], '://', $_SERVER['SERVER_NAME'], $dir, $file, '</strong></p>', "\n";
}
It’s $uploadedFile in your example that I’m trying to get to. It is fine if there’s no sub-directory.
Try this:
$dir = '/newsletter/';
$destdir = '..' .$dir;
if (!is_dir($destdir))
{
mkdir($destdir);
}
$_FILES['uppfile']['tmp_name'] = 'DEBUG_NOT_USED.PDF';
echo '<br>$dir ==> ' ,
$dir = '/newsletter/';
echo '<br>$destdir ==> ' ,
$destdir = '..' .$dir;
echo '<br>$filename ==> ' ,
$filename = 'news1.pdf';
echo '<br>$destpath ==> ' ,
$destpath = $destdir .$filename;
echo '<br>$tmp_name ==> ' ,
$tmp_name = $_FILES['uppfile']['tmp_name'];
if (1 || is_uploaded_file($tmp_name) && move_uploaded_file($tmp_name, $destpath))
{
echo '<p>File ', $filename, ' has been uploaded successfully as <strong>', $filename, '</strong>.</p>', "\n";
echo '<p>Newsletter URL: <strong>'
, $_SERVER['REQUEST_SCHEME']
, '://'
, $_SERVER['SERVER_NAME']
, $dir
, $filename
, '</strong></p>'
, "\n";
}
die;
Output:
$dir ==> /newsletter/
$destdir ==> …/newsletter/
$filename ==> news1.pdf
$destpath ==> …/newsletter/news1.pdf
$tmp_name ==> DEBUG_NOT_USED.PDF
File news1.pdf has been uploaded successfully as news1.pdf .
Newsletter URL: http://localhost/newsletter/news1.pdf
Edit:
Added $dir and $destdir.
Gandalf
November 28, 2019, 1:16pm
7
But what if my script is in a sub-directory in localhost? That is where I’m getting stuck.
If www is considered a sub-directory then this works.
https://www.peacevase.tk/assets/gandalf458/
<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');
$dir = '/newsletter/';
$destdir = '..' .$dir;
if (!is_dir($destdir))
{
mkdir($destdir);
}
$_FILES['uppfile']['tmp_name'] = 'DEBUG_NOT_USED.PDF';
echo '<br>$dir ==> ' ,
$dir = '/newsletter/';
echo '<br>$destdir ==> ' ,
$destdir = dirname( __DIR__) .$dir;
echo '<br>$destdir ==> ' ,
$destdir = strstr($destdir, 'public_html/');
echo '<br>$destdir ==> ' ,
$destdir = trim( substr($destdir, 12) );
echo '<br>$dir ==> ' ,
$dir = strstr($destdir, 'public_html', false);
echo '<br>$filename ==> ' ,
$filename = 'news1.pdf';
echo '<br>$filename ==> ' ,
$filename = 'news1.html';
echo '<br>$destpath ==> ' ,
$destpath = $destdir .$filename;
echo '<br>$tmp_name ==> ' ,
$tmp_name = $_FILES['uppfile']['tmp_name'];
if (1 || is_uploaded_file($tmp_name) && move_uploaded_file($tmp_name, $destpath))
{
echo '<p>File ', $filename, ' has been uploaded successfully as <strong>', $filename, '</strong>.</p>', "\n";
echo '<br> $url ==> ',
$url = $_SERVER['REQUEST_SCHEME']
. '://'
. $_SERVER['SERVER_NAME']
. '/'
. $destpath
// . $filename
;
echo '<p>Newsletter URL: <strong>';
echo '<a href="' .$url .'">' ,$filename .'</a>';
echo '</strong></p>'
, "\n"
;
}
It took me ages to find out why the file nest1.html was not opening. I created the file for testing purposes - the file name had a leadiing space!
Based on your question and the files paths you posted, you would really benefit from setting up a Virtual Host and using an MVC Architecture and Routing.
With a Virtual Host and MVC, your URL would go from
http://www.domain.com/folder/newsletters/news1.pdf
To
http://.myapp.domain.com/newsletters/news1
It would also give you unlimited control over your directory structure and eliminate “sub-directories” as far as the URL goes.
3 Likes
Gandalf
Closed
February 27, 2020, 11:52pm
10
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.