We specify the target directory for the uploaded file in PHP. One var is probably confusing.

Laravel_learn
3 min readDec 21, 2022

--

Sometimes I get confused about small things.

Hey, take a look at this example.

If I have a directory named phpfiles. In this directory if I have only 2 files one is abc.txt and second is abc2.txt.

So if you’re using Windows, you’ve probably seen a box bar at the top.

So let’s say if I want to access a file, I’ll just use this bar.

To access abc.txt I will put C:\Users\example user\Desktop\phpfiles\abc.txt

I have opened this file without any error.

so it’s OK

But if I change this url it will definitely show me the error that this file does not exist.

Same thing happens in php script when we want to put a file in a directory and we put URL of the directory path with filename but the file doesn’t exist yet so why doesn’t it show errors.

Let’s see the example:

$temp_file = $_FILES[‘file’][‘tmp_name’];
$target_dir = ‘/path/to/phpfiles/’;
$target_file = $target_dir . basename($_FILES[‘file’][‘name’]);

After that we used

move_uploaded_file($temp_file, $target_file)

So that means the file doesn’t exist but we are calling it before moving it. $target_file = $target_dir . basename($_FILES[‘file’][‘name’]);

Here, how I solved this confusion. let’s’ see

First, let’s start with the basics. When a user uploads a file using a form on a web page, the file is temporarily stored on the server in a special location. This is done to ensure that the file is fully uploaded before it is moved to its final destination. The temporary location of the uploaded file is specified by the tmp_name element of the $_FILES array, which is a global array that contains all the information about uploaded files.

Now, let’s say you want to move the uploaded file from its temporary location to a new destination on the server. This is where the move_uploaded_file function comes in. This function is used to move an uploaded file from its temporary location to a new location on the server.

To use move_uploaded_file, you need to specify two things: the current location of the file (i.e., the temporary location on the server), and the new location where you want to move the file. The first argument to the function (the current location of the file) is specified using the tmp_name element of the $_FILES array, while the second argument (the new location) is specified using a variable that you define in your script.

1 When a user uploads a file using a form on a web page, the file is temporarily stored on the server in a special location.

2 The PHP $_FILES array is a global array that contains information about the uploaded file, such as its name and temporary location on the server.

3 The move_uploaded_file function is used to move the uploaded file from its temporary location to a new location on the server.

4 To use move_uploaded_file, you need to specify two things: the current location of the file (i.e., the temporary location on the server), and the new location where you want to move the file.

5 The move_uploaded_file function returns true if the file was successfully moved, and false if there was an error.

So our whole confusion is about the $target_file we specified.

$target_file = $target_dir . basename($_FILES[‘file’][‘name’]);

The $target_file variable is used to specify the final destination of the uploaded file on the server. It is constructed by concatenating the $target_dir variable (which specifies the target directory on the server where the file should be moved to) with the base name of the uploaded file (obtained from $_FILES['file']['name']).

The base name of the uploaded file is the file name without any preceding directories. For example, if the file is named “example.txt” and it is located in the “documents” directory, the base name of the file would be “exampled”.

The $target_file variable is used in conjunction with the move_uploaded_file function, which is used to move the uploaded file from its temporary location on the server to the final destination specified by the $target_file variable.

So that was the only point.

At the time the $target_file variable is constructed, the file itself has not yet been moved to the target directory. The $target_file variable is simply being constructed in preparation for the file move, which occurs later in the script when the move_uploaded_file function is called.

--

--